gpt4 book ai didi

c - 链表清除函数

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:43 25 4
gpt4 key购买 nike

我正在尝试使用此函数 clearList 清除程序末尾的链接列表,但我不知道该函数的定义究竟有什么问题。

void clearList(struct node *list) { 
struct node *p;
for(p = list; p != NULL; p = p->next)
free(p);
}

最佳答案

在释放节点之前,您需要获取next 指针:

p = list; //list is the header

while( p != NULL ){
list = list->next;

free(p);

p = list;
}

关于c - 链表清除函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40944515/

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