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. System.Web.Mvc.IAuthorizationFilter.cs

    ylbtech-System.Web.Mvc.IAuthorizationFilter.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutra ...

  2. yolo+keras+tensorflow出错:No module named 'leaky_relu'+

    结论:keras2.1.5+tensorflow1.6.0即可. 首先出现的是:No module named 'leaky_relu',此时把keras改成2.1.5照样出错,改成keras2.1. ...

  3. 1day:了解python

    一.’计算机语言有哪些? 1.开发语言: 高级语言:Python,Java,PHP,C#,C++,(面向字节码) 低级语言:C.汇编(面向机器码) 备注:机器码是直接和计算机硬件沟通,字节码是通过解释 ...

  4. Python操作三大主流数据库✍✍✍

    Python操作三大主流数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库, ...

  5. day 1 预习

    day1 显卡:全称是显示接口卡,又称显示适配器,是电脑进行数模信号转换设备,承担输出显示图形的任务,显卡接在电脑主板上它将电脑的数字信号转换成模拟信号,让显示器显示出来.同时显卡还有图像处理能力,可 ...

  6. linux /bin/find 报错:paths must precede expression 及find应用

    1.问题描述,运行下面的命令,清楚日志 [resin@xx ~]$ ssh xxx  "/usr/bin/find /data/logs/`dirname st_qu/stdout.log` ...

  7. opencv-图像遍历

    #include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> #include&l ...

  8. T2980 LR棋盘【Dp+空间/时间优化】

    Online Judge:未知 Label:Dp+滚动+前缀和优化 题目描述 有一个长度为1*n的棋盘,有一些棋子在上面,标记为L和R. 每次操作可以把标记为L的棋子,向左移动一格,把标记为R的棋子, ...

  9. JavaScript数组的2种定义方式

    JavaScript中没有数组类型,JavaScript中数组是以内置对象的形式存在的. 数组是存储多个值的集合(仓库). JS中定义数组的2种方式: 1.使用new Array()构造函数定义数组 ...

  10. falcon监控指标

    mysql监控指标: 流量状态: Bytes_received/s #平均每秒从所有客户端接收到的字节数,单位KB Bytes_sent/s #平均每秒发送给所有客户端的字节数,单位KB