Pat(Advanced Level)Practice--1052(Linked List Sorting)
来源:互联网 发布:天涯八卦是什么软件 编辑:程序博客网 时间:2024/04/28 04:44
Pat1052代码
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<cstdio>#include<vector>#include<algorithm>#define MAX 100001using namespace std;typedef struct Node{int addr;int key;int next;}Node;bool cmp(const Node &l,const Node &r){if(l.key<r.key)return true;elsereturn false;}int main(int argc,char *argv[]){int n,first;int count;int i,j;int index;Node N[MAX];vector<Node> v;scanf("%d%d",&n,&first);for(i=0;i<n;i++){scanf("%d",&index);scanf("%d%d",&N[index].key,&N[index].next);N[index].addr=index;}index=first;count=0;while(index!=-1){count++;v.push_back(N[index]);index=N[index].next;}sort(v.begin(),v.end(),cmp);if(v.size()==0)printf("0 -1\n");else{printf("%d %05d\n",count,v[0].addr);for(i=0;i<count-1;i++){v[i].next=v[i+1].addr;printf("%05d %d %05d\n",v[i].addr,v[i].key,v[i].next);}printf("%05d %d %d\n",v[count-1].addr,v[count-1].key,-1);}return 0;}
- Pat(Advanced Level)Practice--1052(Linked List Sorting)
- Pat(Advanced Level)Practice--1028(List Sorting)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- PAT (Advanced Level) Practise 1052 Linked List Sorting (25)
- Pat(Advanced Level)Practice--1074(Reversing Linked List)
- 【PAT】【Advanced Level】1052. Linked List Sorting (25)
- PAT (Advanced) 1052. Linked List Sorting (25)
- Pat(Advanced Level)Practice--1083(List Grades)
- 【PAT】【Advanced Level】1028. List Sorting (25)
- Pat(Advanced Level)Practice--1097(Deduplication on a Linked List)
- PAT (Advanced Level) 1052. Linked List Sorting (25) 结构体排序
- 1052. Linked List Sorting (25)【链表+排序】——PAT (Advanced Level) Practise
- Pat(Advanced Level)Practice--1039(Course List for Student)
- Pat(Advanced Level)Practice--1047(Student List for Course)
- 1028. List Sorting (25) @ PAT (Advanced Level) Practise
- 【C++】PAT(advanced level)1028. List Sorting (25)
- PAT (Advanced Level) 1028. List Sorting (25) 结构体排序
- PAT (Advanced Level) Practise 1028 List Sorting (25)
- Java二进制Class文件格式解析
- 菜鸟学习oracle
- VMware虚拟机XP系统安装图文教程
- 异步线程无法多次创建的问题
- 指向指针的学生
- Pat(Advanced Level)Practice--1052(Linked List Sorting)
- gcc configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0
- 第四周作业——图的表示
- JPA 2.0 Concurrency and locking
- 根据年月日计算星期几的函数
- 朴素贝叶斯分类器
- MFC关于消息响应的宏代码
- Longest Common Prefix
- VC数字图像处理编程讲座