gpt4 book ai didi

c - 删除整个链表 C

转载 作者:行者123 更新时间:2023-12-01 08:19:44 26 4
gpt4 key购买 nike

我的程序使用链表按顺序输入数字。

My input: 2 4 23 34 534 543 

当我去删除列表时,我得到这个:

137429056 137428992 137429040 137429024 137429008 0 

这是为什么?

void deleteList(node* head)
{
if(head == NULL)
printf("List is empty\n");

else
deleteList(head->next);

free(head);
}

最佳答案

您释放了内存,但您没有将任何链接(或 head 本身)设置为 null,因此您引用的是未分配的内存。

另外:当 while 循环更简单时,为什么要使用递归?

关于c - 删除整个链表 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15490593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com