1052. Linked List Sorting (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. pat1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  2. PAT 解题报告 1052. Linked List Sorting (25)

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

  3. Linked List Sorting (链表)

    Linked List Sorting (链表)   A linked list consists of a series of structures, which are not necessari ...

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

  5. PAT 1052 Linked List Sorting [一般]

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

  6. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  7. Linked List Sorting

    静态链表(用结构体数组模拟链表)     1052 Linked List Sorting (25分)   A linked list consists of a series of structur ...

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

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

  9. 1052. Linked List Sorting (25)

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

随机推荐

  1. 动态获取html页面的内容,并且取其中的某块元素的方法

     $.ajax({  url: "http://192.168.1.59:8888/app-tpl-webapp/tpl/design.html",  async:false, ...

  2. 谈谈java虚拟机

    本文可作为北京圣思元深入java虚拟机的课堂笔记. 先看一个令人dan teng的面试题 public class Singleton { public static Singleton s=new ...

  3. c/c++ 表达式求值

    表达式求值 [问题描述] 一个算术表达式是由操作数(operand).运算符(operator)和界限符(delimiter)组成的.假设操作数是正整数,运算符只含加减乘除等四种运算符,界限符有左右括 ...

  4. 数据结构-C语言递归实现树的前中后序遍历

    #include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...

  5. 用Xstream时候遇到的两个小异常

    第一个 com.thoughtworks.xstream.converters.ConversionException: Cannot construct ClassXXX as it does no ...

  6. [Zabbix3.0] 添加MySQL监控

    zabbix3.0 server已经自带MySQL的模板了,只要修改agent端,然在web端给主机添加模板就好了. Agent端操作 /etc/zabbix/zabbix_agentd.d/user ...

  7. React Native入门 认识Flexbox布局

    Flexbox布局是由W3C在09年提出的在Web端取代CSS盒子模型的一种布局方式. ReactNative实现了Flexbox布局的大部分功能. Flexbox布局所使用的属性,基本可以分为两大类 ...

  8. log4j日志的配置

    在项目开发中,记录错误日志方便调试.便于发现系统运行过程中的错误.便于后期分析, 在java中,记录日志有很多种方式,比如说log4j log4j需要导入的包: commons-loggin.jar ...

  9. jQuery选择器面试题

    $("#myELement")    选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...

  10. Java框架-Spring MVC理解001

    Spring MVC理解 1.servlet--Spring MVC的本质 2.Spring MVC其实是一个工具,具体的理解可以分为两步:第一步,了解这个工具是怎么创建出来的:第二步,了解这个工具是 ...