鏈表本身是一個數據結構,清空是把鏈表中的元素清空,但鏈表還存在,銷毀則是把鏈表這個結構的內存都釋放了。。
清空是鏈表沒節點,但是鏈表還在,可以繼續插入節點。銷毀就是鏈表沒了,整個鏈表的空間都被釋放了,不能進行任何操作了。
就像一個杯子,把杯子里的水倒掉叫清空,把杯子砸碎叫銷毀。。
清空鏈表與銷毀鏈表的代碼如下:
#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; }