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 ...
随机推荐
- Java-HttpServletRequest
//继承了ServletRequest接口,给servlet提供Request请求信息,servlet 容器会创建以后HttpServletRequest对象 //并把它作为一个参数给service函 ...
- win32 线程通信初步
// 线程通信机制.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #define NUM_THREADS 10 #include < ...
- C语言之将无符号字符型转化为ascii码值
这个宏是在linux内核中获取的,主要的功能是能够将一个无符号字符型的参数转化为ASCII码值. ASCII : ASCII 编码里包括了128个字符.用 十进制 0 到 127 来表示 .那就对了 ...
- OpenCV——照亮边缘
具体的算法原理可以参考: PS滤镜,照亮边缘 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_ ...
- android 获取Bitmap位图所占用的内存大小
今天在看Universal-Image-Loader源码的时候,在对图片的超过用户在所设的阈值的时候,系统会调用GC将LinkHashMap比较靠底层的图片引用去掉,这里涉及到一个技术单个图片的文图大 ...
- 安卓学习笔记一 Activity延迟转跳实现欢迎界面
新人学习安卓,为了刚好的学习,现做如下笔记..同时希望自己的经验可以帮助新人们学习入门. 几乎每个app都有个欢迎界面,我们可以使用Activity转跳来实现. 首先建立一个MainActivity ...
- How tomcat works 读书笔记十三 Host和Engine
Host Host是Context的父容器.如果想在一个tomcat上部署多个context就需要使用Host了.上下文容器的父容器是主机,但是可能有一些其它实现,没有必要的时候也可以忽略.不过在实践 ...
- 深入了解Map
联系 Java中的Map类似于OC的Dictionary,都是一个个键值对组成,一键对应一值.我在之前的文章中讲解过Set,其实在JAVA底层Set依赖的也是Map,那我们都知道,Set是单列的(只有 ...
- Mongodb3.6 基操命令(二)——如何使用help
前言 在上一篇文章Mongodb3.6 快速入门(一)中,我们主要使用两个命令: 1.mongod #启动服务 2.mongo #连接mongodb 对于刚接触mongo的人来说,该怎么给命令传递参数 ...
- java安全——BASE64
这个主题主要是关于java安全的,应该来说算是个大杂烩吧,但是又不缺乏实用性,算是作为一个总结,用的时候可以作为参考. 1.使用BASE64加解密 在java加密技术中,BASE64算是一种最简单.最 ...