C链接列表销毁功能 [英] C Linked-list Destroy Function
本文介绍了C链接列表销毁功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ $ p $
void destroy(struct node * n){
if(!n)return;
销毁(n->下一个);
free(n);
n = NULL; }
预先致谢
解决方案
void deleteList(struct node ** head_ref)
{
struct node * current = * head_ref;
struct node * next;
while(current!= NULL){
next = current-> next;
free(current);
current = next;
}
* head_ref = NULL;
}
像这样尝试....您可以根据需要更改名称。如果您仍然需要帮助,请告诉我。
I'm trying to learn C, and as many people, I've been a little stuck with pointers. Anyways, I made a recursive function that destroys my linkedlist, but as I've debugged, when I'm back from the function the head of the list is not null as it should be, so I'm guessing it's some basic misunderstanding with pointers. Here's the function:
void destroy(struct node* n){
if(!n) return;
destroy(n->next);
free(n);
n = NULL; }
Thanks in advance.
解决方案
void deleteList(struct node** head_ref)
{
struct node* current = *head_ref;
struct node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
Try like this ....you can change names as you want. If you still need help let me know.
这篇关于C链接列表销毁功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文