typedef struct _node_t{ int a; struct _node_t* next; }node_t; node_t* Mergesort(node_t* L,int count) //链表头及要链表元数个数 { if(count==1) return L; int count1=count/2; int count2=count-count1; node_t* p=L; int i; for(i=0;i<count1;i++) p=p->next; node_t* h1=Mergesort(L,count1); node_t* h2=Mergesort(p,count2); int t1=0,t2=0; node_t *head=NULL,*cur; while(t1<count1||t2<count2) { if(t1<count1&&t2<count2) { if(h1->a < h2->a) p=h1,h1=h1->next,t1++; else p=h2,h2=h2->next,t2++; } else if(t1<count1) p=h1,h1=h1->next,t1++; else p=h2,h2=h2->next,t2++; if(head==NULL) head=cur=p; else { cur->next=p; cur=p; } } return head; } void sort(node_t* &a) { int count=0; node_t* p=a; while(a) { count++; a=a->next; } a=Mergesort(p,count); p=a; int i; for(i=0;i<count-1;i++) p=p->next; p->next=NULL; } void input(node_t* &a) { a=NULL; int n; node_t* p; while(true) { scanf("%d",&n); node_t* s= new node_t; s->a=n; if(a==NULL) a=p=s; else p->next=s; p=s; p->next=NULL; if (n==0) { break; } } } void output(node_t* a) { while(a) { printf("%d ",a->a); a=a->next; } printf("//n"); } int main() { node_t* a; //freopen("test.in","r",stdin); input(a);//屏幕输入时输一串数字,然后按enter再按ctrl+z结束 sort(a); output(a); return 0; }