写一个函数insert插入一结点。
分析:插入点可能有以下三种情况:在链表中间、表头、表尾。
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);}
点击加载更多评论>>