Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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_Linked List - Fatal编程技术网

C 链表未保存对下一个节点的引用

C 链表未保存对下一个节点的引用,c,linked-list,C,Linked List,我相信这是显而易见的。。。但是我已经做了很长时间了,只是不能发现我的错误,所以希望有一些新的眼睛将钉住它 BOOLEAN init() { struct list *foodList = (struct list*)malloc(sizeof(struct list)); struct node *head = (struct node*)malloc(sizeof(struct node)); head->data = NULL; head->ne

我相信这是显而易见的。。。但是我已经做了很长时间了,只是不能发现我的错误,所以希望有一些新的眼睛将钉住它

BOOLEAN init()
{
    struct list *foodList = (struct list*)malloc(sizeof(struct list));

    struct node *head = (struct node*)malloc(sizeof(struct node));
    head->data = NULL;
    head->next = NULL;

    foodList->head = head;


    if (NULL == foodList) {
        printf("List creation failed");
        return FALSE;
    }


    return TRUE;
}


void add_node(struct list *foods, struct food * newFood) {


    struct node *curr = foods->head;

    while (curr->next != NULL) {
        curr = curr->next;
    }

    curr->next = (struct node*)malloc(sizeof(struct node));
    curr->next->data = newFood;
     printf("%p\n", curr);
    curr->next->next = NULL;


    }
列表和节点一样,只是一个典型的列表结构

发生的情况是每个节点都成功创建(它们的地址为0x7fd1dac03ab0,
0x7fd1dac03ac0等),但节点->下一步在每个节点上返回0x7fd1dac03960。就像我说的,我确信我在做一些非常明显的事情。。。但是任何帮助都将不胜感激。

只需使用现有的许多链表实现中的一个即可。在
init()
函数中定义了
foods
的地方,您已经在这里使用了它:
if(NULL==foods)
。此外,在取消引用之前,您应该检查<代码>食物列表是否是代码> null < /代码>。考虑切换到基于节点的列表实现,而没有明显的<代码>头<代码>。只需将整个列表表示为指向第一个节点的指针。在调用<代码> AddioNoDeNe()/<代码> <代码> In()>代码>时,要认真考虑如何改变头部。如果(NULL==foodList){:太晚了。