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

题目描述:

给一个链表按照node里面存着的key排序.

算法分析:

算法很简单就是排序算法, 调用现成的sort之类的库就行了, 注意两点”

(1) 给定的node不一定都是在同一个链表上 (这就是为什么我们需要head node 的address)

(2) head node address 可能为-1,小心segment fault.

(3) 如果链表是空,应当输出”0 -1″.

#include <iostream>
#include <cstdio>
#include <algorithm> using namespace std;
#define MX 100001
struct Node {
int key, val, next;
};
//不用vector,而用数组的好处是,数组可作为hash
Node m[MX], linked[MX];
bool cmp(Node p, Node q) {
return p.val<q.val;
} int main()
{
int N, head;
scanf("%d%d", &N, &head);
for (int i=; i<N; i++) {
int tmp;
scanf("%d", &tmp);
scanf("%d%d", &m[tmp].val, &m[tmp].next);
m[tmp].key = tmp;
}
int cnt=;
while (head != -) {
linked[cnt++] = m[head];
head = m[head].next;
} sort(linked, linked+cnt, cmp);
if (cnt == ) printf("0 -1");
else {
printf("%d %05d\n", cnt, linked[].key);
for (int i=; i<cnt;i++) {
if (i!=cnt-) {
printf("%05d %d %05d\n", linked[i].key, linked[i].val, linked[i+].key);
}
else {
printf("%05d %d -1", linked[i].key, linked[i].val);
}
}
} return ;
}

PAT 解题报告 1052. Linked List Sorting (25)的更多相关文章

  1. PAT (Advanced Level) 1052. Linked List Sorting (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  2. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  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 1052 Linked List Sorting (25)

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

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

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

  6. PAT Advanced 1052 Linked List Sorting (25) [链表]

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

  7. PAT甲题题解-1052. Linked List Sorting (25)-排序

    三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...

  8. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  9. 1052. Linked List Sorting (25)

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

随机推荐

  1. checkbox 全選、取消全選、反選

    在寫一個全選.取消全選.反選的功能時. 未來元素[type='checkbox'],在使用了jquery-1.11.1.min.js版本的jquery時: 功能版本1: 存在的問題,當使用了attr時 ...

  2. Bootstrap页面布局21 - BS对话框设计

    设计弹出层对话框: 设计一个点击登录按钮,再弹出一个登陆对话框的实例,且带有动画效果 <div class='container-fluid'> <h2 class='page-he ...

  3. xml提取

    $url = 'http://221.232.141.108/hsdcw/news.xml'; $opts = array( 'http'=>array( 'method'=>" ...

  4. SET GLOBAL long_query_time=0

    SHOW VARIABLES LIKE '%long%'

  5. linux卸载php

    卸载: rpm -qa | grep php 所以正确的卸载顺序是:# rpm -e php-mysql-5.1.6-27.el5_5.3  --allmatches #同名全部卸载# rpm -e ...

  6. 区分总结innerHeight与clientHeight、innerWidth与clientWidth、scrollLeft与pageXOffset等属性

    window对象: (1)innerHeight属性:窗口中文档显示区域的高度,不包括菜单栏.工具栏等部分.该属性可读可写. IE不支持该属性,IE中body元素的clientHeight属性与该属性 ...

  7. Windows下mysql自动备份的最佳方案

    网上有很多关于window下Mysql自动备份的方法,其实不乏一些不好的地方和问题,现总结出一个最好的方法供大家参考: 新建一个记事本,然后重命名为: mysql_backup.bat 然后单击右键选 ...

  8. (转)js闭包初入门

    先看一段JS代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 function a(){             var num = 0;             function  ...

  9. 使用OC语言编写两个超大数相乘或相加的算法的思路和超大正整数相乘的代码

    正文: 在编程中,无论是OC还是C亦或是C++语言,所声明的整数变量都会在内存中占有固定的存储空间,而这些存储空间都是固定的. 比如我们知道的int.long.short.unsigend int.u ...

  10. net use

    net use * \\ipaddr\share "password" /user:"username" /persistent:yes 在运行中输入“\\”+ ...