1052 Linked List Sorting
2016-02-28 20:05
302 查看
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
解题思路:注意两个情况:不知一条链表,还有链表为空的情况,所以next属性是有用的。
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (< 105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address is the address of the node in memory, Key is an integer in [-105, 105], and Next is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
解题思路:注意两个情况:不知一条链表,还有链表为空的情况,所以next属性是有用的。
#include<iostream> #include<vector> #include<algorithm> using namespace std; struct node{ int key; int address; int next; }; bool cmp(const node n1, const node n2){ return n1.key < n2.key; } node ns[100005]; int main(){ for (int n, root; scanf("%d%d", &n, &root) != EOF;){ vector<node>nodes; for (int i = 0; i < n; i++){ int address; scanf("%d", &address); scanf("%d %d", &ns[address].key, &ns[address].next); ns[address].address = address; } while (root != -1){ nodes.push_back(ns[root]); root = ns[root].next; } sort(nodes.begin(), nodes.end(),cmp); if (nodes.size()){ printf("%d %05d\n", nodes.size(), nodes[0].address); for (int i = 0; i < nodes.size() - 1; i++){ printf("%05d %d %05d\n", nodes[i].address, nodes[i].key, nodes[i + 1].address); } printf("%05d %d -1\n", nodes[nodes.size() - 1].address, nodes[nodes.size() - 1].key); } else{ printf("%d -1\n", nodes.size()); } } return 0; }
相关文章推荐
- c++强类型枚举enum class NEWTYPE
- ASP.NET MVC学习之模型绑定(2)
- [MySQL] 事务隔离级别
- Java 中InputStream与Reader的区别
- OpenStack快速安装
- ASP.NET MVC学习之模型绑定(1)
- 函数返回一个指针
- 【BZOJ3511】土地划分【最小割】
- 怎样在 ubuntu 和 debian 中通过命令行管理 KVM
- 怎样在 ubuntu 和 debian 中通过命令行管理 KVM
- Loadrunner简述
- ASP.NET MVC学习之模型模板篇
- ASP.NET MVC学习之视图篇(2)
- hdu 2639 (第k小的01背包)
- 委托子窗体刷新父窗体
- 同源策略详解及绕过
- c语言指针和地址中 &a 和*a 有啥区别
- 【C/C++学院】0905-boostSocketTCPUDP/虚函数表的调用/复杂表达式
- ASP.NET MVC学习之视图篇(1)
- C语言中的一些容易出现错误的知识点