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. ~/.bashrc的常用alias设置,30 个方便的 Bash shell 别名

    centos6.5/centos7系统中,alias定义在/etc/bashrc,分别写在/etc/profile.d/*.sh中,可以在此目录添加my.sh,或者~/.bashrc,或者~/.bas ...

  2. VS2010-MFC(工具栏:工具栏资源及CToolBar类)

    转自:http://www.jizhuomi.com/software/215.html 上一节讲了菜单及CMenu类的使用,这一节讲与菜单有密切联系的工具栏. 工具栏简介 工具栏一般位于主框架窗口的 ...

  3. ubuntu install redis/mongo 以及 监控安装

    sudo apt-get updatesudo apt-get install redis-server mongodb sudo apt-get install htopsudo apt-get i ...

  4. Django的日常-AJAX

    目录 Django的日常-AJAX AJAX简介 AJAX与JQ的一个实例 AJAX与contentType AJAX传json格式 AJAX传文件 Django的日常-AJAX AJAX简介 首先A ...

  5. springcloud系列13 config的客户端使用

    config客户端的使用: 也是首先要引入依赖: <dependency> <groupId>org.springframework.cloud</groupId> ...

  6. LeetCode 237. 删除链表中的节点(Python3)

    题目: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点. 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: head ...

  7. Django问题

    Django问题 'WSGIRequest' object has no attribute 'user' django python 关注 2 关注 收藏 0 收藏,413 浏览   当我的djan ...

  8. [JZOJ1320] 【Usaco2009 gold 】拯救奶牛

    题目 题目大意 一个三角形的网格图,三角形与其有共同边的三角形相连. 起点到所有终点的最短距离. 思考历程 数据看起来还挺大的,所以不是什么图论算法. 这显然是一个结论题. 什么结论? 然后我就开始推 ...

  9. 洛谷P2526 【SHOI2001】小狗散步

    原题传送门 题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从 ...

  10. php判断一个值是否在一个数组中,区分大小写-也可以判断是否在键中

    function in_array_case($value,$array){ return in_array(strtolower($value),array_map('strtolower',$ar ...