69,373
社区成员
发帖
与我相关
我的任务
分享
//没细看楼主的程序,不过阅读性太差了。if里面都一长串。
//主要思想还是冒泡排序。不过要注意的是结点空的时候。。
void *sort()
{
struct node *ptr1 = head;
struct node *ptr2 = head->next;
int temp;
while(ptr1!=NULL)
{
while(ptr2!=NULL)
{
if(ptr1->num > ptr2->num)
{
temp = ptr1->num;
ptr1->num = ptr2->num;
ptr2->num = temp;
}
ptr2=ptr2->next;
}//while ptr2
ptr1 = ptr1->next;
if(ptr1->next == NULL)
{
break;
}
else
ptr2 = ptr1->next;
}//while ptr1
return head;
}
#include <stdio.h>
if(s!=p) //if p->next.data > p->next->next.data
{
//swap(p-> next,p-> next->next)
t=s->next;
s->next=t->next;
t->next=p->next;
p->next=t;
}
struct num *sort(struct num *head)
{
struct num *p,*q,*t,*s;
p=head;
while(p-> next-> next!=NULL)
{
for(s=p,q=p-> next;q-> next!=NULL;q=q-> next)
{
if(s-> next-> data> q-> next-> data) s=q;
if(s!=p)
{
t=s-> next;
s-> next=t-> next;
t-> next=p-> next;
p-> next=t;
}
}
p=p-> next;
}
return head;
}