1052. Linked List Sorting (25)
2015-12-01 16:39
246 查看
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:
Sample Output:
---------------华丽的分割线----------------
分析:先把输入存下来,然后遍历,把符合条件的收到另一个数组里,最后输出。
代码:
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
---------------华丽的分割线----------------
分析:先把输入存下来,然后遍历,把符合条件的收到另一个数组里,最后输出。
代码:
#include<cstdio> #include<cstdlib> #define Maxn 100001 typedef struct { int address; int key; int next; }Node; Node input[Maxn]; Node result[Maxn]; int cmp(const void *a,const void *b); int main(void) { int N,header; scanf("%d %5d",&N,&header); int i; int inaddr,inkey,innext; for(i=0;i<N;++i) { scanf("%5d %d %d",&inaddr,&inkey,&innext); input[inaddr].address = inaddr; input[inaddr].key = inkey; input[inaddr].next = innext; } int thisnode = header; int realnode = 0; while(thisnode != -1) { result[realnode] = input[thisnode]; thisnode = input[thisnode].next; ++realnode; } if(realnode == 0) { printf("0 -1\n"); return 0; } qsort(result,realnode,sizeof(result[0]),cmp); header = result[0].address; bool firstnode = true; printf("%d %05d\n",realnode,header); for(i=0;i<realnode;++i) { if(firstnode) { printf("%05d %d ",result[i].address,result[i].key); firstnode = false; } else { printf("%05d\n",result[i].address); printf("%05d %d ",result[i].address,result[i].key); } if(i == realnode - 1) { printf("-1\n"); } } system("pause"); return 0; } int cmp(const void *a,const void *b) { Node tempa = *(Node *)a; Node tempb = *(Node *)b; return tempa.key - tempb.key; }
相关文章推荐
- [Canvas绘图] 第08节 "8"解九妹
- jQuery多个倒计时的实现
- C#中获取当前时间:System.DateTime.Now.ToString()用法
- uva-11995 - I Can Guess the Data Structure!(栈,优先队列,队列,水题)
- 从头认识java-11.3 格式化输出(2)
- JQuery 2次点击实现2次不同的响应
- language model
- jdk jvisualvm 或 jconsole 远程监控 tomcat
- Checkstyle配置文件及常见输出结果
- 2000端口号的坑
- 通过两个GPS计算两个GPS点的距离
- Qt学习(1)
- 【转】Vim 常用命令总结
- shell脚本步骤调试
- Starting MySQL. ERROR! Manager of pid-file quit without updating file.
- [KDD,ML]奇异值分解(SVD)的物理意义
- java 流操作
- Util Yeoman
- 数组根据数组中字典的value值排序
- 通过RTMP协议将AVC(H264)数据发送到流媒体服务器