C链接列表销毁功能 [英] C Linked-list Destroy Function

查看:112
本文介绍了C链接列表销毁功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆