PAT1052. Linked List Sorting

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
思路:利用vector是一个很好的方法,另外一点需要注意的就是全部无效的节点。一定要进行特殊输出

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int MAX = 100010;
 7 struct Info
 8 {
 9     int address;
10     int data;
11     int next;
12 };
13 bool cmp(Info A,Info B)
14 {
15
16    return A.data<B.data;
17 }
18
19
20 int main(int argc, char *argv[])
21 {
22     int N,start;
23     scanf("%d%d",&N,&start);
24     vector<Info>in(MAX);
25     vector<Info>out;
26     for(int i=0;i<N;i++)
27     {
28         Info node;
29         scanf("%d%d%d",&node.address,&node.data,&node.next);
30         in[node.address]=node;
31     }
32     int pt=start;
33     while(pt!=-1)
34     {
35         out.push_back(in[pt]);
36         pt=in[pt].next;
37     }
38     sort(out.begin(),out.end(),cmp);
39     if(out.size()!=0)
40       printf("%d %05d\n",out.size(),out[0].address);
41     else
42       printf("%d -1\n",out.size());
43     for(int i=0;i<out.size();i++)
44     {
45         if(i==out.size()-1)
46           printf("%05d %d -1\n",out[i].address,out[i].data);
47         else
48              printf("%05d %d %05d\n",out[i].address,out[i].data,out[i+1].address);
49     }
50
51     return 0;
52 }

时间: 2024-08-11 09:56:32

PAT1052. Linked List Sorting的相关文章

pat1052. Linked List Sorting (25)

1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 a

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 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

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

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

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

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

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