1052. Linked List Sorting (25)
来源:互联网 发布:成都java程序员工资 编辑:程序博客网 时间:2024/05/18 10:38
1052. Linked List Sorting (25)
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 0000111111 100 -100001 0 2222233333 100000 1111112345 -1 3333322222 1000 12345Sample Output:
5 1234512345 -1 0000100001 0 1111111111 100 2222222222 1000 3333333333 100000 -1
#include<iostream>#include<algorithm>using namespace std;struct Node{ int add,data,next;};Node m[100001],link[100001];int cmp(Node a,Node b){ return a.data<b.data;}int main(){ int x,y,first,i,k=0; cin>>x>>first; for(i=0;i<x;i++) { cin>>y; cin>>m[y].data>>m[y].next; m[y].add=y; } while(first!=-1) { link[k++]=m[first]; first=m[first].next; } sort(link,link+k,cmp); if(k==0) { cout<<0<<" "<<-1<<endl; return 0; } printf("%d %05d\n",k,link[0].add); for(i=0;i<k-1;i++) printf("%05d %d %05d\n",link[i].add,link[i].data,link[i+1].add); printf("%05d %d %d\n",link[k-1].add,link[k-1].data,-1); return 0;}
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- 1052. Linked List Sorting (25)
- idea中自动生成serialVersionUID
- JAVA基本类型库
- 关于表格td点击事件的监听
- maya(学习笔记)之材质基础
- PHP验证终端类型是否为手机
- 1052. Linked List Sorting (25)
- Dubbo调度机制解析(LoadBalance扩展)
- HDU-4278 Faulty Odometer 数学递推 || 八进制
- 斯坦福大学公开课:傅里叶变换及其应用
- Android横竖屏切换个人总结
- java流
- Jmeter命令行运行实例讲解
- 建站流程篇——黑客内参教你如何快速学会做网站
- UVa 248 Cutting Corners <计算几何+SPFA>