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 (<) 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 −.

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 [−], 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)题目给出的结点中可能有不在链表中的无效结点

(2)最后一个测试点测试的是首地址为 - 1,即为空链表的情况,此时只输出0 - 1

(3)输出时结点地址除 - 1外要有5位数字,不够则在高位补0 。所以地址 - 1要进行特判输出

 #include <iostream>
#include <map>
using namespace std;
int main()
{
int N, head, maxV = -;
cin >> N >> head;
map<int, int>value;
map<int, pair<int, int>>data;
for (int i = ; i < N; ++i)
{
int addr, val, next;
cin >> addr >> val >> next;
data.insert(make_pair(addr, make_pair(val, next)));
}
if (data.find(head) == data.end())
{
cout << << " " << - << endl;
return ;//该链表为空
}
while (head != -)//遍历一下数据,找到是链表中的数据
{
value.insert(make_pair(data[head].first, head));
head = data[head].second;
}
printf("%d %05d\n", value.size(), value.begin()->second);
for (auto ptr = value.begin(); ptr != value.end(); ++ptr)
{
if (ptr == value.begin())
printf("%05d %d ", ptr->second, ptr->first);
else
printf("%05d\n%05d %d ", ptr->second, ptr->second, ptr->first);
}
printf("%d\n", -);
return ;
}
												

PAT甲级——A1052 Linked List Sorting的更多相关文章

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

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

  2. PAT甲级1052 Linked List Sorting

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...

  3. 【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 ...

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

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

  5. PAT A1052 Linked List Sorting

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

  6. A1052. Linked List Sorting

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

  7. PAT Advanced 1052 Linked List Sorting (25) [链表]

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

  8. 【PAT甲级】1028 List Sorting (25 分)

    题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...

  9. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

随机推荐

  1. SQLServer 中存储过程

    SQLServer 中存储过程返回的三种方式( 包括存储过程的创建, 在存储过程中调用, 在VS中调用的方法)存储过程有三种返回:   1.   用return返回数字型数据   2.   用返回参数 ...

  2. 利用bu命令下延迟断点

    bu可以针对符号下断点.这里是用bu下延迟断点的意义在于即使目标驱动没有被加载,windbg也允许我们针对符号设置断点.当新加载驱动程序后,windbg就会检查驱动程序中是否包含了设置了延迟断点的函数 ...

  3. (34)C#异常

    一.异常的层次结构 二.异常格式 异常的一般格式 try { //可能会抛出异常的代码 } catch { //发现错误后会运行这里面的代码 } finally { //写不论是否出现异常都执行的代码 ...

  4. IENumerable_Test

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  5. day 69 Django基础五之django模型层(一)单表操作

    Django基础五之django模型层(一)单表操作   本节目录 一 ORM简介 二 单表操作 三 章节作业 四 xxx 一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现 ...

  6. IIR滤波器数字频带转换

    <DSP using MATLAB>(Ingle & John Proakis)3ed,书中表8.2似乎不对. <Discrete Time signal processin ...

  7. 有关Tensorboard问题

    先说我的各个版本: 操作系统: win7 64 Python: 3.5 Tensorflow: 1.2 Tensorboard: 1.6 错误一: 只显示Graphs,不显示Histogram和Sca ...

  8. PKU Campus 2015

    PKU Campus 2015 B 注意到竖着落下不改变列模 4 的结果.问题转化为:模 4 系下,给序列,可选长度为 4 子区间,区间加一,能否让所有元素相等. C.Rabbit's Festiva ...

  9. 使用CEfSharp之旅(2) js前台事件执行后台方法

    原文:使用CEfSharp之旅(2) js前台事件执行后台方法 版权声明:本文为博主原创文章,未经博主允许不得转载.可点击关注博主 ,不明白的进群191065815 我的群里问 https://blo ...

  10. 解决jquery ajax在跨域访问post请求的时候,ie9以下无效(包括ie9)的问题

    最近在做项目的时候遇到一个问题,就是跨域请求ajax的时候ie9以下的浏览器不可以访问,直接执行error里面的代码,但是也不报错,就上网查了查,发现了一个很好用的方法,在这里记录一下,也希望可以帮到 ...