我这个效率要高一些,呵呵。
#include
typedef struct listnode
{
int f;
struct listnode *next;
} ListNode;
ListNode *sort(ListNode *head)
{
ListNode *p,*p1,*p2,*p3;
ListNode h, t;
if (head == NULL) return NULL;
h.next=head;
p=&h;
while (p->next!=NULL)
{
p=p->next;
}
p=p->next=&t;
while (p!=h.next)
{
p3=&h;
p1=p3->next;
p2=p1->next;
while (p2!=p)
{
if ((p1->f)>(p2->f))
{
p1->next=p2->next;
p2->next=p1;
p3->next=p2;
p3=p2;
p2=p1->next;
} else {
p3=p1;
p1=p2;
p2=p2->next;
}
}
p=p1;
}
while (p->next!=&t)
{
p=p->next;
}
p->next=NULL;
return h.next;
}
int main() {
ListNode h,j,k,l;
h.next=&j;
h.f=3;
j.next=&k;
j.f=5;
k.next=&l;
k.f=1;
l.next=NULL;
l.f=7;
ListNode* p = sort(&h);
while (p != NULL) {
printf("%d ", p->f);
p=p->next;
}
printf("\n");
return 0;
}
#include
#include
struct number
{
int num;
struct number *next;
};
void main()
{
struct number *head;
struct number *p1,*p2,*p,*p3,*p4;
int n=0,m,i,j;
p1=p2=(struct number *)malloc(sizeof(struct number));
printf("Please enter the number: \n");
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=NULL)
{
n=n+1;
if(head==NULL) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct number *)malloc(sizeof(struct number));
scanf("%d",&p1->num);
}
p2->next=NULL;
p=head;
for(p4=head;p4!=NULL;p4=p4->next)
{
for(p3=head;p3->next!=NULL;p3=p3->next)
{
if(p4->num>p3->num)
{
m=p4->num;
p4->num=p3->num;
p3->num=m;
}
}
p=head;
}
printf("\nNow,there %d numbers are: ",n);
p=head;
printf("\n");
if(head!=NULL)
do
{
printf("%5d ",p->num);
p=p->next;
}while(p!=NULL);
printf("\n");
getch();
}
-_-~
那就用指针交换