在C語言鏈表的插入操作
提問人:楊紫紅發(fā)布時(shí)間:2020-11-17
寫一個(gè)函數(shù)insert插入一結(jié)點(diǎn)。
分析:插入點(diǎn)可能有以下三種情況:在鏈表中間、表頭、表尾。
struct student *insert(struct student *head, struct student *stud )
{ struct student *p0, *p1, *p2;
p1=head; p0=stud;
if(head= =NULL) {head=p0; p0->next=NULL;}
else { while((p0->num>p1->num)&&(p1->next !=NULL))
{ p2=p1; p1=p1->next;}
if(p0->num<=p1->num)
if(head= =p1) {head=p0; p0->next=p1; }
else { p2->next=p0; p0->next=p1; }
else {p1->next=p0; p0->next=NULL;}
}
return(head);}
分析:插入點(diǎn)可能有以下三種情況:在鏈表中間、表頭、表尾。
struct student *insert(struct student *head, struct student *stud )
{ struct student *p0, *p1, *p2;
p1=head; p0=stud;
if(head= =NULL) {head=p0; p0->next=NULL;}
else { while((p0->num>p1->num)&&(p1->next !=NULL))
{ p2=p1; p1=p1->next;}
if(p0->num<=p1->num)
if(head= =p1) {head=p0; p0->next=p1; }
else { p2->next=p0; p0->next=p1; }
else {p1->next=p0; p0->next=NULL;}
}
return(head);}
繼續(xù)查找其他問題的答案?
相關(guān)視頻回答
點(diǎn)擊加載更多評(píng)論>>