當前位置:首頁 » 編程語言 » c語言鏈表的刪除

c語言鏈表的刪除

發布時間: 2023-08-05 23:05:21

㈠ C語言如何刪除鏈表頭節點

這種刪除方法是頭節點存放值的,這樣可以清楚的看到是否刪除掉了頭節點。

用p保存頭節點 p=head;

head指向下一個節點,成為新的頭節點head=p->next;

釋放原來的頭節點 free(p);


#include<iostream>
#include<stdio.h>
#include<stdlib.h>
voidprintList(structnode*head);
structnode*delHead(structnode*head);
structnode{
intdata;
structnode*next;
};
intmain(){
inti;
structnode*tail,*head,*p;
//尾插法插入數據
p=(structnode*)malloc(sizeof(structnode));
p->data=0;
tail=head=p;
tail->next=NULL;
for(i=1;i<10;i++){
p=(structnode*)malloc(sizeof(structnode));
tail->next=p;
p->data=i;
p->next=NULL;
tail=p;
}
printList(head);
head=delHead(head);
printList(head);
system("pause");
return0;
}
//刪除頭結點
structnode*delHead(structnode*head){
structnode*p=head;
head=p->next;
free(p);
returnhead;
}
//列印鏈表
voidprintList(structnode*head){
structnode*p=head;
while(p!=NULL){
printf("%i",p->data);
p=p->next;
}
printf(" ");
}

㈡ C語言中鏈表怎麼刪除結點

有分才有動力啊哥們。

刪除節點很簡單,以單鏈表為例,牢記三點

  1. 避免斷鏈,刪除掉節點後,前一個節點的p->next一定要指向後一個節點(如果是頭節點,記得要將新表頭P指向到原來的第二個節點。如果是尾節點,記得要將新的尾節點p->next置為NULL,)。

  2. 避免野指針,刪除掉節點後,p->next=NULL;

  3. 避免內存泄漏,刪除的節點,要用free釋放堆內存。

如果是雙向鏈表,不過是多了一個對prev操作,道理是一樣的。

熱點內容
androidsdk32下載 發布:2025-08-24 16:36:35 瀏覽:266
安卓手機忘了解鎖密碼怎麼辦 發布:2025-08-24 16:35:09 瀏覽:789
存儲過程在java代碼 發布:2025-08-24 16:32:11 瀏覽:498
寫編譯器需要哪些知識 發布:2025-08-24 16:09:06 瀏覽:330
資料庫的關系符號 發布:2025-08-24 16:02:29 瀏覽:626
sqlserver表值函數 發布:2025-08-24 15:56:27 瀏覽:554
linuxc內存泄露 發布:2025-08-24 15:54:30 瀏覽:112
python讀取文件每一行 發布:2025-08-24 15:32:27 瀏覽:83
abbplc編程軟體 發布:2025-08-24 15:31:43 瀏覽:209
蘋果關閉密碼如何設置新密碼 發布:2025-08-24 14:28:41 瀏覽:299