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 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
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5
 6 #include <math.h>
 7 #include <algorithm>
 8
 9
10 #include <string>
11
12 using namespace std;
13 const int maxn=100010;
14 struct Node{
15
16     int address,data,next;
17     bool flag;
18 }node[maxn];
19 bool cmp(Node a,Node b)
20 {
21     if(a.flag==false||b.flag==false)
22     {
23         return a.flag>b.flag;
24     } else
25     {
26         return a.data<b.data;
27     }
28 }
29
30 int main(){
31    for(int i=0;i<maxn;i++)
32    {
33        node[i].flag=false;
34    }
35    int n,begin,address;
36    scanf("%d %d",&n,&begin);
37
38    for(int i=0;i<n;i++)
39    {
40        scanf("%d",&address);
41        scanf("%d%d",&node[address].data,&node[address].next);
42        node[address].address=address;
43    }
44   int count=0,p=begin;
45   while(p!=-1)
46   {
47       node[p].flag=true;
48       count++;
49       p=node[p].next;
50   }
51   if(count==0)
52   {
53       printf("0 -1\n");
54   }else
55   {
56       sort(node,node+maxn,cmp);
57       printf("%d %05d\n",count,node[0].address);
58       for(int i=0;i<count;i++)
59       {
60           if(i!=count-1)
61           {
62               printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
63           }else
64           {
65               printf("%05d %d -1\n",node[i].address,node[i].data);
66           }
67       }
68   }
69     return 0;
70 }
时间: 2024-10-18 16:14:24

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

A1052 Linked List Sorting (25分)

一.技术总结 这个也是一个链表类题目,主要是在结构提上的设计,可以设计一个flag参数用于记录真实有效的结点. 然后就是根据题目要求进行排序,输出结果. cmp()函数可以,一层是把有效节点排到数组的左边,然后可以进行二次比较,按题目要求来. 二.参考代码 #include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct Node{ int key;//数据 int n

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

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