以插入一个学生记录为例,使插入后学号升序。
struct student *insert(head,stu)
struct student *head,*stu;
{
struct student *p=head,*q;
if(head==NULL)
{
head=stu;
stu->next=NULL;
}
else
{
/*寻找插入点*/
while((stu->num>p->num)&&(p->next!=NULL))
{
q=p;
p=p->next;
}
if(stu->num<=p->num)
{
if(head==p) head=stu; /*插入到第一个结点之前*/
else q->next=stu; /*插入到q所指向的结点之后*/
stu->next=p;
}
else /*插入到最后的结点之后*/
{
p->next=stu;
stu->next=NULL;
}
}
return head;
}