Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在末尾插入链表_C - Fatal编程技术网

C 在末尾插入链表

C 在末尾插入链表,c,C,这是我的linkedlistcode,用于在末尾插入节点。我正在将错误代码转储。所以,请帮助我代码有什么问题 如果我继续回来;在末尾和*headRef=newnode之后的行;它起作用了。那么为什么要返回一个void函数呢 struct node { int data; // node format struct node* next; }; void insertAtEnd(struct node** headRef, int newData) { struct no

这是我的linkedlistcode,用于在末尾插入节点。我正在将错误代码转储。所以,请帮助我代码有什么问题

如果我继续回来;在末尾和*headRef=newnode之后的行;它起作用了。那么为什么要返回一个void函数呢

struct node
{
    int data; // node format
    struct node* next;
};

void insertAtEnd(struct node** headRef, int newData)
{
    struct node* ptr;
    struct node* newnode;
    ptr = (*headRef);
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = newData;
    newnode->next = NULL;
    if (*headRef == NULL)
    {
        *headRef = newnode;
    }
    while (ptr->next != NULL)
    {
        ptr = ptr->next;
    }
    ptr->next = newnode;
}

您应该返回if内部,因为在if语句之后,它再次进入while循环,并在末尾添加额外的节点

struct node
{
    int data; // node format
    struct node* next;
};

void insertAtEnd(struct node** headRef, int newData)
{
    struct node* ptr;
    struct node* newnode;
    ptr = (*headRef);
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = newData;
    newnode->next = NULL;
    if (*headRef == NULL)
    {
        *headRef = newnode;
         return;

    }
    while (ptr->next != NULL)
    {
        ptr = ptr->next;
    }
    ptr->next = newnode;
    return;
}

python与此有何关系?选择一种语言来诊断核心转储我们需要查看完整的程序。在进入
while
循环之前,应该设置
ptr=*headRef
。此外,如果
*hreadRef==NULL
则应
返回
,这样就不会进入以下while循环。1)检查
malloc
是否成功,2)如果head为NULL,则不要执行while循环,其余代码则在
insertAtEnd()中执行