當前位置:首頁 » 編程語言 » 刪除整個鏈表c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

刪除整個鏈表c語言

發布時間: 2022-10-07 23:38:00

c語言中關於鏈表的刪除

所謂鏈表,就是用指針將內存中動態分配的結點空間,鏈接起來成一個表。

所以,建表的過程即是每次為新結點分配內存;因此,釋放空間的話,也要從頭到尾,一個一個結點的釋放,這樣才能全部釋放掉。

這段代碼釋放了整個鏈表空間內存;while循環的作用是從頭到尾釋放後續結點,如果直接free(pHead)則後面的結點將無法找到,那麼造成內存空間泄露。


另外,你的while循環存在一個錯誤,假設釋放了倒數第一個結點後,pHead指向最後一個結點,而最後一個結點的next為NULL,那麼這樣最後一個結點也沒有釋放掉,while就退出了。

while循環應該更正為:

while(pHead!=NULL)
{
pNext=pHead->next;
free(pHead);
pHead=pNext;
}

② C語言中如何用free清除一串鏈表

#include<stdio.h>
#include<stdlib.h>
structNode{
intcon;
Node*next;
};

Node*insert(Node*h,inta,intpos)//這里,增加返回類型
{
Node*p=(Node*)malloc(sizeof(Node));
Node*pt=h;
if(pos==0)
{
p->con=a;
p->next=h;
h=p;
}
else
{
for(inti=1;i<pos;i++)//這里,如果pos值輸入大於節點個數,會導致越界,自己修改
{
pt=pt->next;
}
p->con=a;
p->next=pt->next;
pt->next=p;
}
returnh;//這里,返回地址
}

intmain()
{
inti;
intcount=0;
puts("Enternum");
scanf("%d",&i);
Node*p=(Node*)malloc(sizeof(Node));
Node*head=p;
p->next=NULL;
p->con=i;
Node*c=head;
puts("Nextone,nonnumtostop");
while(scanf("%d",&i)==1)
{
fflush(stdin);
p=(structNode*)malloc(sizeof(structNode));
p->con=i;
c->next=p;
p->next=NULL;
c=p;
puts("Nextone,nonnumtostop");
}
fflush(stdin);

inta,pos;
puts("insertaatpos");
scanf("%d%d",&a,&pos);
head=insert(head,a,pos);//這里,地址傳出來

c=head;
while(c!=NULL)
{
printf("%d%d ",count,c->con);
count++;
c=c->next;
}

c=head;
Node*tmp;
while(c!=NULL)//here,多了個分號,內存釋放需要改
{
tmp=c;
c=c->next;
free(tmp);

}
return0;
}

③ C語言鏈表刪除的問題 不知道怎麼回事鏈表刪不掉 求教!!!!

#include<stdio.h>
#include<stdlib.h>
struct
node
{
int
no;
struct
node
*next;
};
void
display(struct
node
*head)
{
while(head)
{
printf("%d
",head->no);
head=head->next;
}
}
void
del(struct
node
*&head)//這里
參數要使用引用否則當刪除鏈表頭的節點時會造成錯誤
{
int
t;
struct
node
*q,*p;
scanf("%d",&t);
if(head->no==t)
{
p=head;
head=head->next;
free(p);
}
else
{
q=head;
p=head->next;
while(p&&(p->no!=t))
{
q=p;
p=p->next;
}
if(!p)return;
q->next=p->next;
free(p);
}
}
//該函數用於釋放整個鏈表
void
ReleaseAll(struct
node
*&head)
{
struct
node
*pNode;
while
(head
!=
NULL)
{
pNode
=
head;
head
=
head->next;
free(pNode);
}
}
int
main()
{
struct
node
*p,*head=NULL;
int
t;
scanf("%d",&t);
while(t>0)
{
p=(struct
node
*)malloc(sizeof(struct
node));
p->no=t;
p->next=head;
head=p;
scanf("%d",&t);
}
display(head);
del(head);
display(head);
ReleaseAll(head);
display(head);
printf("\n");
system("pause");
}

④ C語言 刪除鏈表

那裡面head和tail都是全局變數。你看仔細點。

⑤ c語言結構體鏈表的節點刪除問題 求助大佬

  • 這個出錯點在行 while() 循環之後的那一句: free(l->tail); 第一段程序,其實在 while() 循環裡面已經把全部節點都釋放了,包括尾節點,所以第一段程序不需要再釋放一次 l->tail,只需要保留 l->tail = NULL; 即可。 第二段程序,因為最後一個...

  • 2019-10-14回答者:隱世高手0074個回答

  • c語言中刪除鏈表指定節點的一些問題?

  • 問:struct student *Del (struct student *head, int num) { struct studen...

  • 答:你仔細看看 while (p1->num != num && p1->next != NULL) 這一句之前的代碼 p2什麼時候賦值過? 而且if (p2 == head) //如果要刪除的節點是第一個節點 既然是第一個節點,那麼 while (p1->num != num && p1->next != NULL) 這個循環一次都沒有執...

  • 2015-10-20回答者:知道網友1個回答2

  • C語言數據結構的鏈表刪除與查找問題~~,實在是被弄...

  • 問:#include "stdlib.h" #include "stdio.h" typedef struct node{ //鏈表...

  • 答:#include #include typedef struct LNode {char character; struct LNode*next; }LNode,*PLNode; PLNode CreateList()/*創建單鏈表*/ {PLNode P,head,q; int i; head=(PLNode)malloc(sizeof(LNode)); p=head; p->next=NULL; for(i=0;icharacter=...

  • 2010-11-25回答者:qjbrh532個回答

  • C語言刪除鏈表結點 結點結構如下

  • 問:#define ELEMTYPE char typdef struct node { ELEMTYPE date; struct no...

  • 答:我提供思路哈,你自己寫一下,這個不難的。 分為兩種情況: 1、刪除的是頭結點,這又可以分為兩種情況:a)若是鏈表只有一個頭結點,那麼刪除後頭結點為NULL;b)若是鏈表不止一個節點,那麼head指針指向頭結點下一個節點。兩種情況都要free 之...

⑥ C語言鏈表刪除

對於鏈表的訪問遍歷,最好是判斷表指針是否為NULL來決定是否繼續,如:
p=head ;
while( p != NULL ) //當結點指針不為空時,遍歷表,這要求建表時,尾結點的next=NULL!
{
printf("num=%d\n", p->num );
p=p->next ;
}
你自己調整一下自己的代碼吧,估計是刪除結點時,按個數訪問時,訪問到了NULL時,你還在操作結點指針。

⑦ C語言鏈表刪除問題

所謂鏈表,就是用指針將內存中動態分配的結點空間,鏈接起來成一個表。
所以,建表的過程即是每次為新結點分配內存;因此,釋放空間的話,也要從頭到尾,一個一個結點的釋放,這樣才能全部釋放掉。
這段代碼釋放了整個鏈表空間內存;while循環的作用是從頭到尾釋放後續結點,如果直接free(pHead)則後面的結點將無法找到,那麼造成內存空間泄露。
另外,你的while循環存在一個錯誤,假設釋放了倒數第一個結點後,pHead指向最後一個結點,而最後一個結點的next為NULL,那麼這樣最後一個結點也沒有釋放掉,while就退出了。

⑧ C語言刪除鏈表問題

用這個程序到處回答問題 發財了我 你看下原碼了 ,不懂可以問我的 ,必須要採納哦
理解二級指針就都好辦了
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student //定義節點類型
{
char name[20];
int age;
struct student *next;
};
typedef struct student * list; //定義節點的指針類型
void insert(list *ll,int age,char name[]) //插入元素
{
list p=(list)malloc(sizeof(struct student));
p->age=age;
strcpy(p->name,name);
p->next=NULL;
while(*ll!=NULL)
{
ll=&((*ll)->next);
}
*ll=p;
}
void remove(list *ll,char name[]) //ll為指向指針的指針,name[]為你要刪除的姓名
{
list p=*ll; //定義p指向鏈表的頭指針
while((p=*ll)!=NULL)
{
if(strcmp(p->name,name)==0) // 等於進入,刪除節點後不往下跳
{
*ll=(*ll)->next; //刪除鏈表中一個節點
delete p;//釋放內存
}
else
ll=&((*ll)->next); //不等於往下跳一個節點
}

}
int main()
{
list ll=NULL; //定義頭指針 賦值空
insert(&ll,20,"lona"); //插入元素
insert(&ll,20,"lona");
insert(&ll,20,"lona");
insert(&ll,20,"lona");
insert(&ll,20,"hello");
remove(&ll,"lona"); //刪除相同的元素
while(ll!=NULL) //列印結構 僅僅調試而已,這樣做會破壞鏈表 建議用臨時指針來列印
{
printf("%d,%s\n",ll->age,ll->name);
ll=ll->next;
}

}

⑨ C語言鏈表 刪除

structLNode*delete(LNode*head)
{
LNode*node=head;
LNode**parent=&head;
doublemin,max;
printf("請輸入min和max:");
scanf("%lf%lf",&min,&max);
while(node)
{
//大於min和小於max,則刪除節點
if(node->data>min&&node->data<max)
{
*parent=node->next;
//如果你的LNode是malloc出來的,需要free(node);
node=*parent;
}
else
{
node=node->next;
parent=&node->next;
}
}
returnhead;
//這個邏輯不需要給你太多注釋,你只需要拿一支筆,手動畫一個3個節點的鏈表,
//然後從函數執行開始,一步一步去推怎麼執行的就懂了。
//至於你的程序的錯誤,沒幫你看
}

如何刪除鏈表(C語言).

把頭節點拆下,把剩下的結點當兩個,第一個作為第一個,剩下的作為第二個,先刪除第一個,在刪除第二個,直到第二個為空