鏈表的銷毀與清空(轉)


鏈表本身是一個數據結構,清空是把鏈表中的元素清空,但鏈表還存在,銷毀則是把鏈表這個結構的內存都釋放了。。

        清空是鏈表沒節點,但是鏈表還在,可以繼續插入節點。銷毀就是鏈表沒了,整個鏈表的空間都被釋放了,不能進行任何操作了。

        就像一個杯子,把杯子里的水倒掉叫清空,把杯子砸碎叫銷毀。。

        清空鏈表與銷毀鏈表的代碼如下:

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM