如题,复习期末时敲的,感觉敲的有点纯
/*
typedef struct List{
int i;
struct List* next;
}s;
*/
s* BubbleSort(s*head)
{
if(head==NULL||head->next==NULL) //如果链表为空或只有一个节点,直接返回
return head;
if(head->next->next==NULL) //对只有两个节点的单独处理
{
if(head->i>head->next->i)
{
head->next->next=head;
head=head->next;
head->next->next=NULL;
}
return head;
}
//链表至少有三个节点
s*pleft=head,*p=pleft->next,*pright=p->next,*tem;
int flag=1;
while(flag)
{
pleft=head,p=pleft->next,pright=p->next;
if(pleft->i>p->i)//因为之后的排序只能将除首节点之外的节点排序,所以我在这里对首节点进行处理
{
head=p;
p->next=pleft;
pleft->next=pright;
//此时三指针顺序为p pleft pright
tem=p;
p=pleft;
pleft=tem;
}
flag=0;
while(pright)
{
if(p->i>pright->i)
{
pleft->next=pright;
p->next=pright->next;
pright->next=p;
//此时三个指针的顺序为pleft,pright,p
tem=p;
p=pright;
pright=tem;
flag=1;
}
pleft=p;
p=pright;
pright=pright->next;
}
}
return head;
}