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
 #include<cstdio>
#include<iostream>
#include<algorithm>
typedef struct NODE{
int lt, rt;
int data;
int valid;
NODE(){
valid = ;
}
}node;
node nds[];
bool cmp(node a, node b){
if(a.valid != b.valid){
return a.valid > b.valid;
}else return a.data < b.data;
}
using namespace std;
int main(){
int N, head, addr;
scanf("%d%d", &N, &head);
for(int i = ; i < N; i++){
scanf("%d", &addr);
nds[addr].lt = addr;
scanf("%d %d", &nds[addr].data, &nds[addr].rt);
}
int pt = head, cnt = ;
while(pt != -){
nds[pt].valid = ;
cnt++;
pt = nds[pt].rt;
}
sort(nds, nds + , cmp);
if(cnt > )
printf("%d %05d\n", cnt, nds[].lt);
else printf("0 -1");
for(int i = ; i < cnt; i++){
if(i == cnt - ){
printf("%05d %d -1\n", nds[i].lt, nds[i].data);
}else{
printf("%05d %d %05d\n", nds[i].lt, nds[i].data, nds[i + ].lt);
}
}
cin >> N;
return ;
}

总结:

1、题意:将所给的节点重新排序。 但注意,仅仅是合法节点进行排序。合法节点是指按照第一行所给的首地址能串在一起的所有节点。题目会给孤立的非法节点,需要注意区分。办法就是对合法节点标记1,非法节点标记0。然后利用排序进行分类,以及二级排序。

2、当没有合法节点时,要注意输出特殊情况 0 -1。

3、在读入每一个节点的时候,不要忘记给节点的地址赋值。

A1052. Linked List Sorting的更多相关文章

  1. PAT A1052 Linked List Sorting (25 分)——链表,排序

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...

  2. PAT甲级——A1052 Linked List Sorting

    A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...

  3. PAT A1052 Linked List Sorting

    题意:给出N个结点的地址address.数据域data以及指针域next,然后给出链表的首地址,要求把在这个链表上的结点按data值从小到大输出.样例解释:按照输入,这条链表是这样的(结点格式为[ad ...

  4. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  5. Linked List Sorting (链表)

    Linked List Sorting (链表)   A linked list consists of a series of structures, which are not necessari ...

  6. PAT1052:Linked List Sorting

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  7. 【PAT】1052 Linked List Sorting (25)(25 分)

    1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...

  8. PAT 1052 Linked List Sorting [一般]

    1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...

  9. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

随机推荐

  1. curator 分布式锁InterProcessMutex

    写这篇文章的目的主要是为了记录下自己在zookeeper 锁上踩过的坑,以及踩坑之后自己的一点认识; 从zk分布式锁原理说起,原理很简单,大家也应该都知道,简单的说就是zookeeper实现分布式锁是 ...

  2. java.lang.Exception: No tests found matching Method tes(com.bw.test.Testrefiect) from org.junit.vintage.engine.descriptor.RunnerRequest@3bfdc050 at org.junit.internal.requests.FilterRequest.getRunner

    junit   方法  没有加上注解  @Test java.lang.Exception: No tests found matching Method tes(com.bw.test.Testre ...

  3. PAT L2-002 链表去重

    https://pintia.cn/problem-sets/994805046380707840/problems/994805072641245184 给定一个带整数键值的链表 L,你需要把其中绝 ...

  4. Eclipse布局问题小记

    当Eclipse的Debug,Console(简称工具条)页面被误操作到占据整行时,通过点击工具条的非选项卡部分,然后向代码区域拖动,即可得恢复非单独行模式.

  5. Apache的Thrift引发的编译思考

    最近偶然看到了Apache的Thrift,感觉有点像Corba架构后的变种(赞一个,Facebook真伟大).WSDL能生成C#和Java的(SOAP标准接口,做WebService都用过).Corb ...

  6. [转帖] 从零开始编写自己的C#框架(27)——什么是开发框架

    从零开始编写自己的C#框架(27)——什么是开发框架 http://www.cnblogs.com/EmptyFS/p/4105713.html 没写过代码 一直不清楚 框架的含义 不过看了一遍 也没 ...

  7. Android控件第7类——对话框

    1.AlertDialog AlertDialog用来生成对话框,功能十分强大. AlertDialog可以分成4个组成部分:标题栏上的图标,标题区,文本区,按钮区. 使用方法: 创建AlertDia ...

  8. Python——反射

    反射的定义:使用字符串类型的名字 去操作变量 hasattr 函数 与getatter配合使用,用来判断变量是否存在 if hasatter(my,'a'):#如果为真执行 getatter(my,' ...

  9. Lodop图片输出ADD_PRINT_IMAGE 有白边

    ADD_PRINT_IMAGE输出图片,如果使用img标签(即超文本<img标签),是超文本,无论是相对路径,网络图片,还是base64,都可能有白边,这可能和超文本解析有关.ADD_PRINT ...

  10. NVIDIA面目生成器再做突破

    导读 NVIDIA创建的AI系统“GAN”可以通过对图像数据库的学习,来随机生成超逼真人脸照片而一炮走红,经过长时间的研发与晚上目前这套系统已经有了极大的进步.除了可以自主学习之外,生成的内容逼真,让 ...