现在的位置: 首页 > 综合 > 正文

链表的销毁与清空

2013年10月15日 ⁄ 综合 ⁄ 共 1289字 ⁄ 字号 评论关闭

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

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

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

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

  1. #include "stdlib.h"
      
  2. #include "stdio.h"
      
  3.   
  4. struct student  
  5. {  
  6.     int num;              //学号 
      
  7.     float score;          //分数,其他信息可以继续在下面增加字段
      
  8.     struct student *next;       //指向下一节点的指针
      
  9. };  
  10.   
  11. //销毁链表
      
  12. int DestroyList(struct student *head)  
  13. {  
  14.     struct student *p;  
  15.     if(head==NULL)  
  16.         return 0;  
  17.     while(head)  
  18.     {  
  19.         p=head->next;  
  20.         free(head);  
  21.         head=p;  
  22.     }  
  23.     return 1;  
  24. }  
  25.   
  26. //清空链表
      
  27. int ClearList(struct student *head)  
  28. {  
  29.     struct student *p,*q;  
  30.     if(head==NULL)  
  31.         return 0;  
  32.     p=head->next;  
  33.     while(p!=NULL)  
  34.     {  
  35.         q=p->next;  
  36.         free(p);  
  37.         p=q;  
  38.     }  
  39.     head->next=NULL;  
  40.     return 1;  
  41. }  

 

抱歉!评论已关闭.