链表排序,求改错!!!
若原数据输入为1,2,3,4,则在vc中执行时出现错误,不能显示排序后的数据linklist *sort_linklist(linklist *head)
{
linklist *p,*q,*s,*former;
p=head->next;
head->next=NULL;
while(p!=NULL)
{
q=p;
p=p->next;
q->next=NULL;
if(head->next==NULL)
head->next=q;
else
{
s=head->next;
while(q->data>s->data&&s!=NULL)
{
former=s;
s=s->next;
}
if(s==NULL)
{
q->next=NULL;
former->next=q;
}
else
{
if(s==head->next)
{
q->next=s;
head->next=q;
}
else
{
q->next=s;
former->next=q;
}
}
}
}
return head;
}