PAT 1052. Linked List Sorting (25)

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 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

此题需要注意的题目中所给的所有节点并非全部都在链表上,所以应该通过所给的start来去除不在链表上的节点

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <sstream>
 5 #include <algorithm>
 6
 7 using namespace std;
 8
 9 struct Node
10 {
11     int address;
12     int value;
13     int next;
14 };
15
16 bool cmp(const Node& lhs, const Node& rhs)
17 {
18     return lhs.value < rhs.value;
19 }
20
21 Node nodes[1000000];
22
23 int main()
24 {
25     vector<Node> vec;
26     int NodeNum, start;
27     cin >> NodeNum >> start;
28
29     for (int i = 0; i < NodeNum; i++)
30     {
31         Node tmp;
32         cin >> tmp.address >> tmp.value >> tmp.next;
33         nodes[tmp.address] = tmp;
34     }
35     int i = start;
36     while (i != -1)
37     {
38         vec.push_back(nodes[i]);
39         i = nodes[i].next;
40     }
41
42     sort(vec.begin(), vec.end(), cmp);
43     cout << vec.size() << " ";
44     for (int i = 0; i < vec.size(); i++)
45     {
46         printf("%.05d\n", vec[i].address);
47         //cout << vec[i].address << endl;
48         //cout << vec[i].address << " " << vec[i].value << " ";
49         printf("%.05d %d ", vec[i].address, vec[i].value);
50     }
51     cout << -1;
52 }
时间: 2024-10-20 07:36:37

PAT 1052. Linked List Sorting (25)的相关文章

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

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, y

【PAT甲级】1052 Linked List Sorting (25分)

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, yo

PAT甲题题解-1052. Linked List Sorting (25)-排序

三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include <algorithm> #include <cstdio> #include <string.h> using namespace std; const int maxn=100000+5; struct Node{ int addr; int val; int to; bo

PAT Advanced 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 s

PAT:1052. Linked List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int address; int data; int next; bool tag; }Node[100066]; bool cmp(node a,node b) { if(a.tag!=b.tag) return a.tag>b.tag; else return a.data<

PAT (Advanced Level) 1052. Linked List Sorting (25)

简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using namespace std;

【PAT甲级】1052 Linked List Sorting (25 分)

题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组成.以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址. trick: 题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗... 数据点1包含有些结点并非在

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 stru

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 stru