链表本身是一个数据结构,清空是把链表中的元素清空,但链表还存在,销毁则是把链表这个结构的内存都释放了。。

        清空是链表没节点,但是链表还在,可以继续插入节点。销毁就是链表没了,整个链表的空间都被释放了,不能进行任何操作了。

        就像一个杯子,把杯子里的水倒掉叫清空,把杯子砸碎叫销毁。。

        清空链表与销毁链表的代码如下:

#include "stdlib.h"  
#include "stdio.h"  
  
struct student  
{  
    int num;              //学号   
    float score;          //分数,其他信息可以继续在下面增加字段  
    struct student *next;       //指向下一节点的指针  
};  
  
//销毁链表  
int DestroyList(struct student *head)  
{  
    struct student *p;  
    if(head==NULL)  
        return 0;  
    while(head)  
    {  
        p=head->next;  
        free(head);  
        head=p;  
    }  
    return 1;  
}  
  
//清空链表  
int ClearList(struct student *head)  
{  
    struct student *p,*q;  
    if(head==NULL)  
        return 0;  
    p=head->next;  
    while(p!=NULL)  
    {  
        q=p->next;  
        free(p);  
        p=q;  
    }  
    head->next=NULL;  
    return 1;  
}  

 

相关文章:

  • 2022-12-23
  • 2022-03-08
  • 2022-12-23
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2021-07-17
  • 2021-11-14
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
相关资源
相似解决方案