1052 Linked List Sorting (25)(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 Nextpointer 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 (< 10^5^) 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 [-10^5^, 10^5^], 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

C++代码如下:

 #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std; #define maxn 100010 struct Node {
int address;
int key;
int next;
bool flag = false;
}node[maxn]; bool cmp(const Node &a, const Node &b) {
if (a.flag == false || b.flag == false)
return a.flag > b.flag;
else
return a.key < b.key;
} int main() {
int n, add;
scanf("%d %d", &n, &add);
int begin;
for(int i=;i<n;i++){
scanf("%d", &begin);
scanf("%d %d", &node[begin].key, &node[begin].next);
node[begin].address = begin;
}
int cont=,p=add;
while (p != -) {
cont++;
node[p].flag = true;
p = node[p].next;
}
sort(node, node + maxn, cmp);
if (cont == ) {
printf("0 -1\n");
return ;
}
printf("%d %05d\n", cont, node[].address);
for (int i = ; i < cont-; i++) {
printf("%05d %d %05d\n", node[i].address, node[i].key, node[i+].address);
}
printf("%05d %d -1\n", node[cont - ].address, node[cont - ].key);
return ;
}

【PAT】1052 Linked List Sorting (25)(25 分)的更多相关文章

  1. PAT 1052 Linked List Sorting [一般]

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

  2. Pat 1052 Linked List Sorting (25)

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

  3. PAT A1052 Linked List Sorting (25 分)——链表,排序

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

  4. PAT 1052. Linked List Sorting

    这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发.估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些case没过 #include <iost ...

  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 解题报告 1052. Linked List Sorting (25)

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

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

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

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

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

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

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

随机推荐

  1. linux服务器mysql数据库新建数据库并配置数据库用户

    第一步:进入数据库 mysql -uroot -p 提示输入密码,输入你的root用户密码(默认不显示) 如下图: 第二步:创建一个数据库 create database 数据库名称 ;(注意分号结尾 ...

  2. SpringBoot设置事务隔离等级

    "If you're gonna play the game, boy, ya gotta learn to play it right" Spring Boot 使用事务非常简单 ...

  3. 异步查询json传日期格式到前台,变成了时间戳的格式

    问题: 使用mybatis 查询mysql数据库,其中一个日期格式的字段,由异步查询使用 json传递到前台,变成了时间戳,而不是日期格式了.如何使查询出的日期展示成日期格式呢 解决办法: 1.尝试使 ...

  4. 【刷题】LOJ 6002 「网络流 24 题」最小路径覆盖

    题目描述 给定有向图 \(G = (V, E)\) .设 \(P\) 是 \(G\) 的一个简单路(顶点不相交)的集合.如果 \(V\) 中每个顶点恰好在 \(P\) 的一条路上,则称 \(P\) 是 ...

  5. 【BZOJ1093】[ZJOI2007]最大半联通子图(Tarjan,动态规划)

    [BZOJ1093][ZJOI2007]最大半联通子图(Tarjan,动态规划) 题面 BZOJ 洛谷 洛谷的讨论里面有一个好看得多的题面 题解 显然强连通分量对于题目是没有任何影响的,直接缩点就好了 ...

  6. QWidget窗体中使用Q_OBJECT后无法添加背景图片或背景色

    在继承自QWiget的窗体中,设置背景图片或背景色比较简单的方法是使用setStyleSheet()函数,比如在构造函数中可以这样来设置背景图片: this->setStyleSheet(&qu ...

  7. Zookeeper可视化工具

    zkui 简介 zkui它提供了一个管理界面,可以针对zookeepr的节点值进行CRUD操作,同时也提供了安全认证. 下载安装 项目地址 下载 $ git clone https://github. ...

  8. NEGOUT: SUBSTITUTE FOR MAXOUT UNITS

    NEGOUT: SUBSTITUTE FOR MAXOUT UNITS Maxout [1] units are well-known and frequently used tools for De ...

  9. Comparing Differently Trained Models

    Comparing Differently Trained Models At the end of the previous post, we mentioned that the solution ...

  10. WebViewJavascriptBridge测试示例

    android或ios:app与html5通信解决方案 下面只是前端示例代码,后端代码请参考: git https://github.com/marcuswestin/WebViewJavascrip ...