数据结构实验之链表六:有序链表的建立
2016-09-12 20:02
489 查看
数据结构实验之链表六:有序链表的建立Time Limit: 1000MS Memory Limit: 65536KBSubmit Statistic
Problem Description
输入N个无序的整数,建立一个有序链表,链表中的结点按照数值非降序排列,输出该有序链表。Input
第一行输入整数个数N;第二行输入N个无序的整数。Output
依次输出有序链表的结点值。Example Input
6 33 6 22 9 44 5
Example Output
5 6 9 22 33 44
#include <bits/stdc++.h> using namespace std; struct node { int data; node *next; }; struct node *creat(int n) { node *head, *tail, *p; head = new node; head->next = NULL; tail = head; for(int i = 0; i < n; i++) { p = new node; cin >> p->data; p->next = NULL; tail->next = p; tail = p; } return (head); } void Sort(node *head) { node *p, *q; for(p = head->next; p; p=p->next) { for(q = p->next; q; q=q->next) { if(p->data > q->data) { int t = p->data; p->data = q->data; q->data = t; } } } } void display(node *head) { node *p; for(p = head->next; p; p = p->next) { if(p->next) cout << p->data << " "; else cout << p->data << endl; } } int main() { int n; node *head; cin >> n; head = creat(n); Sort(head); display(head); return 0; }
相关文章推荐
- [C/C++]反转链表
- C#数据结构之顺序表(SeqList)实例详解
- C#实现基于链表的内存记事本实例
- Lua教程(七):数据结构详解
- C#模拟链表数据结构的实例解析
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- C++利用静态成员或类模板构建链表的方法讲解
- C++实现简单的学生管理系统
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Linux内核链表实现过程
- C++链表倒序实现方法
- C#通过链表实现队列的方法