C语言 C:如何释放链表中的节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6417158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: How to free nodes in the linked list?
提问by insumity
How will I free the nodes allocated in another function?
我将如何释放在另一个函数中分配的节点?
struct node {
int data;
struct node* next;
};
struct node* buildList()
{
struct node* head = NULL;
struct node* second = NULL;
struct node* third = NULL;
head = malloc(sizeof(struct node));
second = malloc(sizeof(struct node));
third = malloc(sizeof(struct node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
return head;
}
I call the buildList function in the main()
我在 main() 中调用 buildList 函数
int main()
{
struct node* h = buildList();
printf("The second element is %d\n", h->next->data);
return 0;
}
I want to free head, second and third variables.
Thanks.
我想释放头部,第二个和第三个变量。
谢谢。
Update:
更新:
int main()
{
struct node* h = buildList();
printf("The element is %d\n", h->next->data); //prints 2
//free(h->next->next);
//free(h->next);
free(h);
// struct node* h1 = buildList();
printf("The element is %d\n", h->next->data); //print 2 ?? why?
return 0;
}
Both prints 2. Shouldn't calling free(h) remove h. If so why is that h->next->data available, if h is free. Ofcourse the 'second' node is not freed. But since head is removed, it should be able to reference the next element. What's the mistake here?
两个打印 2. 不应该调用 free(h) remove h。如果是这样,为什么 h->next->data 可用,如果 h 是免费的。当然,“第二个”节点没有被释放。但是由于 head 被移除,它应该能够引用下一个元素。这里有什么错误?
回答by insumity
An iterative function to free your list:
一个迭代函数来释放你的列表:
void freeList(struct node* head)
{
struct node* tmp;
while (head != NULL)
{
tmp = head;
head = head->next;
free(tmp);
}
}
What the function is doing is the follow:
该函数正在执行的操作如下:
check if
headis NULL, if yes the list is empty and we just returnSave the
headin atmpvariable, and makeheadpoint to the next node on your list (this is done inhead = head->next- Now we can safely
free(tmp)variable, andheadjust points to the rest of the list, go back to step 1
检查是否
head为 NULL,如果是,则列表为空,我们只返回将 保存
head在一个tmp变量中,并head指向列表中的下一个节点(这是在head = head->next- 现在我们可以安全地
free(tmp)变量了,head只需指向列表的其余部分,回到步骤 1
回答by elcuco
Simply by iterating over the list:
只需遍历列表:
struct node *n = head;
while(n){
struct node *n1 = n;
n = n->next;
free(n1);
}
回答by Bradley Swain
You could always do it recursively like so:
你总是可以像这样递归地做:
void freeList(struct node* currentNode)
{
if(currentNode->next) freeList(currentNode->next);
free(currentNode);
}
回答by Mehul
One function can do the job,
一个功能可以完成这项工作,
void free_list(node *pHead)
{
node *pNode = pHead, *pNext;
while (NULL != pNode)
{
pNext = pNode->next;
free(pNode);
pNode = pNext;
}
}
回答by Manolis
struct node{
int position;
char name[30];
struct node * next;
};
void free_list(node * list){
node* next_node;
printf("\n\n Freeing List: \n");
while(list != NULL)
{
next_node = list->next;
printf("clear mem for: %s",list->name);
free(list);
list = next_node;
printf("->");
}
}

