数据结构实验之链表六:有序链表的建立
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表。
输入
第一行输入整数个数N;
第二行输入N个无序的整数。
第二行输入N个无序的整数。
输出
依次输出有序链表的结点值。
示例输入
6 33 6 22 9 44 5
示例输出
5 6 9 22 33 44
提示
不得使用数组!
- #include<stdio.h>
- struct node
- {
- int data;
- node *next;
- }*head, *p, *q, *tail;
- int main()
- {
- int n;
- scanf("%d", &n);
- head=new node;
- head->next=NULL;
- tail=head;
- for(int i=0;i<n;i++)
- {
- q=new node;
- q->next=NULL;
- scanf("%d", &q->data);
- tail->next=q;
- tail=q;
- }
- int t;
- for(q=head->next;q!=NULL;q=q->next)
- {
- for(p=q->next;p!=NULL;p=p->next)
- {
- if(q->data>p->data)
- {
- t=q->data;
- q->data=p->data;
- p->data=t;
- }
- }
- }
- p=head->next;
- while(p)
- {
- printf("%d", p->data);
- if(p->next)
- printf(" ");
- p=p->next;
- }
- return 0;
- }
1576

被折叠的 条评论
为什么被折叠?



