有序链表的建立(用冒泡排序对连表排序)

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

Time Limit: 1000MS Memory limit: 65536K

题目描述

输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表。

输入

第一行输入整数个数N;
第二行输入N个无序的整数。

输出

依次输出有序链表的结点值。

示例输入

6
33 6 22 9 44 5

示例输出

5 6 9 22 33 44

提示

不得使用数组!
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct vode
 4 {
 5     int date;
 6     struct vode *next;
 7 };
 8 int main()
 9 {
10     struct vode *head,*tail,*p,*q;
11     head=(struct vode *)malloc(sizeof(struct vode ));
12     head->next=NULL;
13     tail=head;
14     int n,i;
15     scanf("%d",&n);
16     for(i=1;i<=n;i++)
17     {
18         p=(struct vode *)malloc(sizeof(struct vode ));
19         p->next=NULL;
20         scanf("%d",&p->date);
21         tail->next=p;
22         tail=p;
23     }
24     int k=0;
25     while(k=!k)
26     {
27         p=head->next;
28         q=p->next;
29         while(q)
30         {
31             if(p->date>q->date)
32             {
33                 int t;
34                 t=p->date;
35                 p->date=q->date;
36                 q->date=t;
37                 k=0;
38             }
39             else
40             {
41                 p=p->next;
42                 q=q->next;
43             }
44         }
45     }
46     p=head->next;
47     int s=0;
48     while(p)
49     {
50         if(s==0)
51         {
52             printf("%d",p->date);
53             s=1;
54         }
55         else printf(" %d",p->date);
56         p=p->next;
57     }
58     return 0;
59 }
View Code
原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3264608.html