链表-什么会导致使用malloc分配的地址从未实际分配?

链表-什么会导致使用malloc分配的地址从未实际分配?,c,data-structures,memory-management,linked-list,singly-linked-list,C,Data Structures,Memory Management,Linked List,Singly Linked List,我正在创建一个链表,但在下面的某个地方,我没有正确地分配地址以将它们链接在一起 没有编译时错误,但是当我试图打印list.head->next节点时,代码中的错误变得很明显。打印结果是地址00000000 为什么未将list.head->next分配给 #include <stdlib.h> #include <stdio.h> struct Node { int data; struct Node* next; }; struct List {

我正在创建一个链表,但在下面的某个地方,我没有正确地分配地址以将它们链接在一起

没有编译时错误,但是当我试图打印
list.head->next
节点时,代码中的错误变得很明显。打印结果是地址
00000000

为什么未将
list.head->next
分配给

#include <stdlib.h>
#include <stdio.h>


struct Node 
{
    int data;
    struct Node* next;
};

struct List 
{
    struct Node* head;
} list;


void insert_node(struct Node *new_node) 
{
    if (list.head == NULL)
    {
        list.head = new_node;
    }

    else 
    {
        struct Node *temp = list.head->next;

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

        temp = new_node;  
    }
}

struct Node* create_node(int data)
{
    static int node_number = 0;

    struct Node * new_node= malloc(sizeof(struct Node));
    new_node->data = data;
    new_node->next = NULL;

    printf("Node inside function %d - address:  %p\n", node_number++, new_node);

    return new_node;
}


int main()
{
    struct Node *a = create_node(11);
    struct Node *b = create_node(12);

    insert_node(a);
    insert_node(b);

    printf("%p\n", list.head->next);
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
结构列表
{
结构节点*头部;
}名单;
无效插入节点(结构节点*新节点)
{
if(list.head==NULL)
{
list.head=新节点;
}
其他的
{
结构节点*temp=list.head->next;
while(temp!=NULL)
{
温度=温度->下一步;
}
temp=新节点;
}
}
结构节点*创建节点(int数据)
{
静态int节点_编号=0;
结构节点*新节点=malloc(sizeof(结构节点));
新建_节点->数据=数据;
新建节点->下一步=空;
printf(“函数%d内的节点-地址:%p\n”,节点号++,新节点);
返回新的_节点;
}
int main()
{
结构节点*a=创建节点(11);
结构节点*b=创建节点(12);
插入_节点(a);
插入_节点(b);
printf(“%p\n”,list.head->next);
}

在else语句的函数insert_节点内

else {
        struct Node *temp = list.head->next;

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

        temp = new_node;  
    }
未连接到列表的局部变量temp已更改

你需要写作

else {
        struct Node *temp = list.head;

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

        temp->next = new_node;  
    }
请注意,让函数依赖于全局变量
list
,这是一个坏主意。在这种情况下,函数无法处理其他列表。因此,对于每个列表,例如在程序中使用两个列表时,您需要编写一个单独的函数来复制代码

该函数至少可以按如下方式定义,如下所示

void insert_node( struct List *list, struct Node *new_node ) 
{
    struct Node **current = &list->head;

    while ( *current != NULL ) current = &( *current )->next;

    *current = new_node;
}

struct Node * create_node( int data )
{
    static unsigned int node_number = 0;

    struct Node *new_node = malloc( sizeof( struct Node ) );

    if ( new_node != NULL )
    {
        new_node->data = data;
        new_node->next = NULL;

        printf("Node inside function %u - address:  %p\n", node_number++, ( void * )new_node);
    }

    return new_node;
}

在函数内部,在else语句中插入_节点

else {
        struct Node *temp = list.head->next;

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

        temp = new_node;  
    }
未连接到列表的局部变量temp已更改

你需要写作

else {
        struct Node *temp = list.head;

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

        temp->next = new_node;  
    }
请注意,让函数依赖于全局变量
list
,这是一个坏主意。在这种情况下,函数无法处理其他列表。因此,对于每个列表,例如在程序中使用两个列表时,您需要编写一个单独的函数来复制代码

该函数至少可以按如下方式定义,如下所示

void insert_node( struct List *list, struct Node *new_node ) 
{
    struct Node **current = &list->head;

    while ( *current != NULL ) current = &( *current )->next;

    *current = new_node;
}

struct Node * create_node( int data )
{
    static unsigned int node_number = 0;

    struct Node *new_node = malloc( sizeof( struct Node ) );

    if ( new_node != NULL )
    {
        new_node->data = data;
        new_node->next = NULL;

        printf("Node inside function %u - address:  %p\n", node_number++, ( void * )new_node);
    }

    return new_node;
}

在else部分中,您迭代列表到其末尾,并为临时值分配新值, 它没有任何效果,因为temp是您的局部变量,您应该在最后一个变量之前迭代到一个,并将值设置为temp->next

#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
结构列表
{
结构节点*头部;
}名单;
无效插入节点(结构节点*新节点)
{
if(list.head==NULL)
{
list.head=新节点;
}
其他的
{
结构节点*temp=list.head;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
temp->next=新节点;
}
}
结构节点*创建节点(int数据)
{
静态int节点_编号=0;
结构节点*新节点=malloc(sizeof(结构节点));
新建_节点->数据=数据;
新建节点->下一步=空;
printf(“函数%d内的节点-地址:%p\n”,节点号++,新节点);
返回新的_节点;
}
int main()
{
结构节点*a=创建节点(11);
结构节点*b=创建节点(12);
插入_节点(a);
插入_节点(b);
printf(“%p\n”,list.head->next);
}

在else部分中,将列表迭代到其末尾,并分配新的临时值, 它没有任何效果,因为temp是您的局部变量,您应该在最后一个变量之前迭代到一个,并将值设置为temp->next

#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
};
结构列表
{
结构节点*头部;
}名单;
无效插入节点(结构节点*新节点)
{
if(list.head==NULL)
{
list.head=新节点;
}
其他的
{
结构节点*temp=list.head;
while(临时->下一步!=NULL)
{
温度=温度->下一步;
}
temp->next=新节点;
}
}
结构节点*创建节点(int数据)
{
静态int节点_编号=0;
结构节点*新节点=malloc(sizeof(结构节点));
新建_节点->数据=数据;
新建节点->下一步=空;
printf(“函数%d内的节点-地址:%p\n”,节点号++,新节点);
返回新的_节点;
}
int main()
{
结构节点*a=创建节点(11);
结构节点*b=创建节点(12);
插入_节点(a);
插入_节点(b);
printf(“%p\n”,list.head->next);
}

您认为应该将其分配到代码中的哪个位置?当您使用调试器跟踪它时,为什么不会发生这种情况?设置
temp=new\u node不会更改列表中任何节点的
下一个
成员。调试器没有告诉我任何我可以亲自解释答案的内容。它编译并运行,这是我困惑的一部分。在
insert\u node
函数中,while循环将继续,直到它为
new\u node
找到可用的插槽。如果
list.head->next
可用,则循环中断,并将新的_节点分配给该点。我做了两个insert的实例,所以
列表。头
是第一次插入时分配的,但是现在
列表.head->next
下一个可用插槽分配不正确。您认为它应该分配到代码中的哪个位置?当您使用调试器跟踪它时,为什么不会发生这种情况?设置
temp=new\u node不会更改列表中任何节点的
下一个
成员。调试器没有告诉我任何我可以亲自解释答案的内容。它编译并运行,这是我困惑的一部分。在
insert\u node
函数中,while循环将继续,直到它为
new\u node
找到可用的插槽。如果