當前位置:首頁 » 編程語言 » 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操作,道理是一樣的。

熱點內容
網吧u盤拒絕訪問 發布:2025-05-16 14:13:50 瀏覽:260
無線網檢查網路配置是怎麼回事 發布:2025-05-16 14:04:03 瀏覽:220
網路爬蟲python代碼 發布:2025-05-16 14:03:26 瀏覽:516
汽車小組件怎麼弄到安卓桌面 發布:2025-05-16 13:51:12 瀏覽:220
linuxg編譯器下載 發布:2025-05-16 13:50:58 瀏覽:776
centosc編譯器 發布:2025-05-16 13:50:17 瀏覽:948
安卓手機如何變換桌面 發布:2025-05-16 13:39:33 瀏覽:515
sql存儲過程命令 發布:2025-05-16 13:17:54 瀏覽:146
用紙做解壓小玩具西瓜 發布:2025-05-16 13:04:09 瀏覽:936
區域網xp無法訪問win7 發布:2025-05-16 13:03:58 瀏覽:943