简体   繁体   English

我正在尝试在链接列表中插入一个新的起始/头节点...注释掉的 function 不起作用,而它下面的那个起作用

[英]I am trying to insert a new starting/head node in a linked list...The commented out function doesn't work while the one below it does the job

// void insert_start(struct node *head, int data)
// {
//     struct node *ptr = (struct node *)malloc(sizeof(struct node));
//     ptr->next_ptr = head;
//     ptr->data = data;
//     head=ptr;
// }

The above function does not work while the one below works上面的 function 不起作用,而下面的起作用

struct node *insert_start(struct node *head, int data)
{
    struct node *ptr = (struct node *)malloc(sizeof(struct node));
    ptr->next_ptr = head;
    ptr->data = data;
    return ptr;
}

see you problem is as follows, in the commented code, when you pass the pointer called head to your function, another pointer pointing to the same address will be created, but you cannot change what does the original pointer in your main points to, it's like in the next picture:看到你的问题如下,在注释代码中,当你将名为head的指针传递给你的 function 时,将创建另一个指向相同地址的指针,但你不能改变你的 main 中的原始指针指向什么,它是如下图所示:

在此处输入图像描述

if you want to change what does your pointer in main function points to, then you have to pass a pointer to that pointer in order to change what does it points, to be something like the next picture:如果要更改主 function 中的指针指向的内容,则必须将指针传递给该指针以更改其指向的内容,如下图所示:

在此处输入图像描述

and in order to do that, you can modify your commented function as follow:为此,您可以修改评论的 function 如下:

void insert_start(struct node **head, int data)
{
     struct node *ptr = (struct node *)malloc(sizeof(struct node));                
     ptr->next_ptr = *head;
     ptr->data = data;
     *head = ptr;
}

and in you main function when you call the function, pass to it the address of the pointer to change what does it point to like: insert_start(&head, data);当您调用 function 时,在您的主要 function 中,将指针的地址传递给它以更改它指向的内容: insert_start(&head, data);

The both functions declared like这两个函数都声明为

void insert_start(struct node *head, int data);

deal with a copy of the value of the pointer to the head node used as an argument expression.处理指向用作参数表达式的头节点的指针的值的副本。

So changing the copy within the functions does not influence on the value of the original pointer.因此,在函数中更改副本不会影响原始指针的值。 It stays unchanged.它保持不变。

The only difference between the functions is that the second function returns to the caller the modified value of the copy of the original pointer to the head node.函数之间的唯一区别是第二个 function 将原始指针副本的修改后的值返回给调用者指向头节点。 So assigning the returned value from the function to the original pointer to the head node you can update its value.因此,将 function 的返回值分配给指向头节点的原始指针,您可以更新其值。

Regarding your first (commented) example...关于您的第一个(评论)示例...
As mentioned in comments, C passes arguments by value.如评论中所述,C 按值传递 arguments。
In C, if the value of a variable is to be changed, then the address of that variable must be passed as opposed to the variable itself.在 C 中,如果要更改变量的值,则必须传递该变量的地址,而不是变量本身。

So in your original function:所以在你原来的 function 中:

void insert_start(struct node *head, int data)
{
    struct node *ptr = (struct node *)malloc(sizeof(struct node));
    ptr->next_ptr = head;
    ptr->data = data;
    head=ptr;
}

*head contains the value of the address of an instance of struct node . *head包含struct node实例的地址值。 As such, head will not be changed upon return.因此,返回时head不会改变。

Should you want to use a form of the void function that will modify the argument to allow it to change the address contained in *head , then you must pass it's address: **head .如果您想使用void function 的形式修改参数以允许它更改包含在*head中的地址,那么您必须传递它的地址: **head Then in the body of the function, make the following changes.然后在 function 的主体中,进行如下修改。 (note reason cast has been removed .) (注意原因演员已被删除。)

void insert_start(struct node **head, int data)
 {
     struct node *ptr = malloc(sizeof(*ptr));
     if(ptr)//verify return of malloc before using
     {
        ptr->data = data;
        ptr->next_ptr = (*head);
        (*head) = ptr;//setting head to address of new node
     }
 }

Calling example: (called in a function such as main() )调用示例:(在 function 中调用,例如main()

struct node *new;
insert_start(&new, 10);//passing address of *new
if(!new)
{ 
    //handle error 
}
....
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用给定的递归在单链表中插入节点:1. 指向头节点的指针 2. 插入新节点的索引 - How can I insert a node in a singly linked list using recursion given: 1. a pointer to the head node 2. the index to insert the new node 为什么在反转 SLL 时将 **head 发送到函数工作,而 *head 在 C 中不起作用? - Why does sending **head to a function work while reversing an SLL and *head doesn't in C? 当我想添加某些内容作为链接列表的开头时,为什么我的代码不起作用? - Why doesn't my code work when I want to append something to be head of the linked list? 我的功能没有记住链接列表的开头 - My function doesn't memorize the head of linked list 我正在尝试扫描一个字符串并将其放入 function,但它不起作用 - I am trying to scanf a string and put it in a function, but it doesn't work 尝试将节点插入链表时出现分段错误 - Segmentation fault while trying to insert node into a linked list 我正在尝试读取和打印链表中的一个值,但我的程序没有给出任何输出 - i am trying to read and print one value in a linked list , but my program does not give any output 为什么我的代码未在此链接列表中插入新节点? - Why does my code not insert a new node into this linked list? 按插入的降序插入链表的功能 - 仅限头节点 - Insert function for linked list in descending order of insertion - only head node permited 在链表中插入节点的函数 - Function to insert node in linked list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM