PAT1052:Linked List Sorting
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 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.先储存所有输入节点到一个dic中(map模拟)。
2.根据给定的头结点遍历链表并将访问到的节点插入一个新的序列(vector)中。这个过程能筛掉dic中不在链表中的节点。
3.对新序列根据key值排序。
4.按照新排列的顺序修改每个节点指向的next地址
5.输出:
1)注意地址格式为标准5位数
2)如果新序列为空,输出"0 -1"。
3)最后末尾节点的next地址直接输出-1,而不是5位的格式"-00001"。
代码
#include<vector>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;
class node
{
public:
int address;
int key;
int next;
};
map<int,node> dic;
vector<node> nodes;
bool cmp(const node& a,const node& b)
{
return a.key < b.key;
} int main()
{
int N,head;
while(cin >> N >> head)
{
for(int i = 0;i < N;i++)
{
int ad;
cin >> ad;
dic[ad].address = ad;
cin >> dic[ad].key >> dic[ad].next;
}
while(head != -1)
{
nodes.push_back(dic[head]);
head = dic[head].next;
}
sort(nodes.begin(),nodes.end(),cmp);
int len = nodes.size();
if(len == 0)
{
cout << "0 -1" << endl;
continue;
}
for(int i = 1;i < len;i++)
{
nodes[i - 1].next = nodes[i].address;
}
nodes[len - 1].next = -1;
head = nodes[0].address;
//output
printf("%d %05d\n",len,head);
for(int i = 0;i < len;i++)
{
if(i == len - 1)
printf("%05d %d -1\n",nodes[i].address,nodes[i].key);
else
printf("%05d %d %05d\n",nodes[i].address,nodes[i].key,nodes[i].next);
}
}
}
PAT1052:Linked List Sorting的更多相关文章
- pat1052. Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- Linked List Sorting (链表)
Linked List Sorting (链表) A linked list consists of a series of structures, which are not necessari ...
- 【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 ...
- PAT 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- Linked List Sorting
静态链表(用结构体数组模拟链表) 1052 Linked List Sorting (25分) A linked list consists of a series of structur ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
随机推荐
- Visual Studio 2010利用libxl读写excel表格数据
C++读写数据,一般通过txt文件,但是随着数据量的增大,采集数据时运用excel表格的优势得以逐步体现.本文主要介绍一下运用第三方库libxl,对excel表格数据进行读写.分为三个部分,第一部分是 ...
- Android群英传笔记——第七章:Android动画机制和使用技巧
Android群英传笔记--第七章:Android动画机制和使用技巧 想来,最 近忙的不可开交,都把看书给冷落了,还有好几本没有看完呢,速度得加快了 今天看了第七章,Android动画效果一直是人家中 ...
- 使用JS取得焦点(focus)元素
原文链接: Get the Focused Element with JavaScript 原文日期: 2014年3月19日 翻译日期: 2014年3月21日 翻译人员: 铁锚 对于良好的用户体验来说 ...
- OC语言(四)
二十八.id类型(万能指针) 可以指向任何id对象(本身就是指针,不用*) id相当于NSObject *,类似于一种多态. 二十九.重写构造方法 new方法的实质:分配空间+alloc 和 初始化- ...
- android JNI调用机制
JNI的出现使得开发者既可以利用Java语言跨平台.类库丰 富.开发便捷等特点,又可以利用Native语言的高效. JNI是JVM实现中的一部分,因此Native语言和Java代码都运行在JVM的宿主 ...
- Zeromq自连接错误
Zeromq自连接错误(金庆的专栏)Zeromq消息中间件开发的服务器和客户端不必按顺序启动,客户端可以在服务器开启之前启动.这是Zmq特别好用的一大特性.利用该特性,网游各功能服务器可以任意重启,实 ...
- Understanding the Objective-C Runtime
Wednesday, January 20, 2010 Understanding the Objective-C Runtime The Objective-C Runtime is one of ...
- RHEL 6 mdadm 实现Soft Raid
环境:RHEL 6.9 x64 1.mdadm命令用于管理系统软件RAID硬盘阵列 格式为:"mdadm [模式] <RAID设备名称> [选项] [成员设备名称]". ...
- 春天的事务之9.3编程式事务 - 跟我学spring3
9.3编程式事务 9.3.1编程式事务概述 所谓编程式事务指的是通过编码方式实现事务,即类似于JDBC编程实现事务管理. Spring框架提供一致的事务抽象,因此对于JDBC还是JTA事务都是采用相同 ...
- 《深入理解JAVA虚拟机》笔记1
java程序运行时的内存空间,按照虚拟机规范有下面几项: )程序计数器 指示下条命令执行地址.当然是线程私有,不然线程怎么能并行的起来. 不重要,占内存很小,忽略不计. )方法区 这个名字很让我迷惑. ...