struct?node?*delete(struct?node*?head)//刪除函數(shù)
創(chuàng)新互聯(lián)建站專注于牙克石網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供牙克石營(yíng)銷型網(wǎng)站建設(shè),牙克石網(wǎng)站制作、牙克石網(wǎng)頁(yè)設(shè)計(jì)、牙克石網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造牙克石網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供牙克石網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
{
printf("請(qǐng)輸入要?jiǎng)h除的學(xué)生姓名");
char?k[100];
scanf("%s",?k);
struct?node?*pre?=?NULL;
struct?node?*q???=?head;
while?(q)?{
if?(strcmp(q-data.name,?k)?==?0){
if?(pre)
pre-next?=?q-next;
else?
head?=?q-next;
free(q);
break;
}
pre?=?q;
q?=?q-next;
}
return?head;
}
temp=p;
p=p-next;
temp-next=NULL;
這三句存在問題,temp=p,讓temp指向p所指向的節(jié)點(diǎn),p=p-next,p指向后移
temp-next=NULL,讓temp的后繼為空,這里出了問題,鏈表從temp指向的節(jié)點(diǎn)斷開,相當(dāng)于刪除p之后的所有節(jié)點(diǎn)。
應(yīng)該先判斷p是不是最后節(jié)點(diǎn)
if(p-next==NULL)
如果是,只好去找p的前趨pre,讓pre-next=NULL,free(p)
如果不是最后節(jié)點(diǎn),將p的后繼節(jié)點(diǎn)數(shù)值域復(fù)制給p,然后將p的后繼節(jié)點(diǎn)刪除,等同與刪除p
p-data=p-next-data;
p-next=p-next-next;
free(p);
剛學(xué)C語言呢,就是看不出來這個(gè)問題,其實(shí)問題很簡(jiǎn)單,就是你在C語言的函數(shù)里面?zhèn)魅肓艘粋€(gè)值,是的它是一個(gè)值,你看到的你傳了一個(gè)指針進(jìn)去,其實(shí)這個(gè)指針本身也是一個(gè)值,鏈表的頭結(jié)點(diǎn)是個(gè)指針,你要改變這個(gè)指針就要用指針的指針才能改變,指針變量也是一個(gè)變量,你傳入一個(gè)指針?biāo)仓皇窃诤瘮?shù)的作用域里面過了一份拷貝!看程序!
/*你想改變a的值,所以你傳了一個(gè)指針進(jìn)去*/
void?change(int?*a)
{
*a?=?10;
}
int?main()
{
int?a?=?0;
change(a);
}
這里要說的是其實(shí),指針也是一個(gè)變量;所以你想改變一個(gè)指針的值,同樣的你也要把這個(gè)指針的地址傳進(jìn)去,就是指針的指針了,看代碼!
void?changePtr(int*?*a)
{
*a?=?(int*)malloc(sizeof(int));
}
int?main()
{
int?a?=?10,*p?=?a;
changePtr(p);
}
上面的兩個(gè)代碼我也沒測(cè)!就是舉個(gè)例子!
看下面的代碼!就是你這個(gè)鏈表的!或者直接打開下面網(wǎng)址(包含下面代碼輸出結(jié)果)
#define?size?5
#define?del1?"one"
#define?del5?"five"
#define?del?"none"
#define?del3?"three"
typedef?struct?VIDEO?{
char?name[20];
struct?VIDEO?*next;
}?video;
/*video?*head;*/
void?build(video**head)?{
int?i?=?0;
video?*temp;
char?*ss[5]?=?{"one","two","three","four","five"};
temp?=?*head?=?NULL;
for(i?=?0;i??size;i++)?{
if(*head)?{
temp-next?=?(video*)malloc(sizeof(video));
temp?=?temp-next;
/*scanf("%s",temp-name);*/
strcpy(temp-name,ss[i]);
temp-next?=?NULL;
}?else?{
*head?=?(video*)malloc(sizeof(video));
/*scanf("%s",head-name);*/
strcpy((*head)-name,ss[i]);
(*head)-next?=?NULL;
temp?=?*head;
}
}
}
int?delete(video**head,char?*str)?{
video?*cur,*prv?=?*head;
if(*head?==?NULL)?return?0;
if(strcmp((*head)-name,str)?==?0)?{
*head?=?(*head)-next;
free(prv);
return?1;
}
cur?=?prv-next;
while(cur??strcmp(cur,str))?{
cur?=?cur-next;
prv?=?prv-next;
}
if(cur)?{
prv-next?=?cur-next;
free(cur);
return?1;
}?else?{
return?0;
}
}
void?show(video?*head)?{
if(head)?{
printf("%s",head-name);
while(head-next)?{
head?=?head-next;
printf("-%s",head-name);
}
printf("\n");
}
}
int?main()
{
video?*head;
build(head);
show(head);
delete(head,del1);
show(head);
delete(head,del5);
show(head);
delete(head,del);
show(head);
delete(head,del3);
show(head);
return?0;
}
輸出結(jié)果為:
one-two-three-four-five
two-three-four-five
two-three-four
two-three-four
two-four
網(wǎng)站題目:c語言函數(shù)刪除鏈表結(jié)點(diǎn) C語言鏈表的刪除
文章網(wǎng)址:http://www.chinadenli.net/article4/doogpoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、響應(yīng)式網(wǎng)站、企業(yè)建站、面包屑導(dǎo)航、營(yíng)銷型網(wǎng)站建設(shè)、用戶體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)