链表的销毁与清空(转)

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

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

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

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

#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;  
}  

猜你喜欢

转载自www.cnblogs.com/stones-dream/p/9938391.html
今日推荐