2121=数据结构实验之链表六:有序链表的建立

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct node
 4 {
 5     int a;
 6     struct node*next;
 7 };
 8 int main()
 9 {
10     int n,d;
11     scanf("%d",&n);
12     struct node*head,*p,*taile,*q;
13     head=(struct node*)malloc(sizeof(struct node));
14     head->next=NULL;
15     taile=head;
16     int i;
17     for(i=0; i<n; i++)
18     {
19         p=(struct node*)malloc(sizeof(struct node));
20         scanf("%d",&p->a);
21         p->next=NULL;
22         taile->next=p;
23         taile=p;
24     }
25     while(n--)
26     {//链表的排序其实和数组的排序差不多,都是一个与它的前一个或者后一个相比较,然后置换。
27         for(p=head->next; p->next!=NULL; p=p->next)
28         {
29             q=p->next;
30             if(p->a >q->a)
31             {
32                 d=p->a;
33                 p->a=q->a;
34                 q->a=d;
35             }
36         }
37     }
38    for(p=head->next;p!=NULL;p=p->next)
39     {
40         if(p->next==NULL)printf("%d
",p->a);
41         else printf("%d ",p->a);
42     }
43     return 0;
44 }
原文地址:https://www.cnblogs.com/Angfe/p/10449439.html