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 (<) 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 −.

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 [−], 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

题意:

给出一个链表,将链表排序,然后把链表上的结点按照data值的从小到大顺序输出。

题解:

数组模拟链表

先后使用两种不同的存储方式。

注意:

1.不一定所有的节点都在链表里(一开始没仔细读题,只得了21分)

2.链表中没有元素时候第一行不输出,第二行输出 0 -1。

AC代码:

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
int n;
struct node{
int k;
int next;
}a[];
struct node2{
int k;
int add;
}b[];
int x;
bool cmp(node2 x,node2 y){
return x.k<y.k;
}
int main(){
int root;
cin>>n>>root;
a[root].next=-;
for(int i=;i<=n;i++){
cin>>x;
cin>>a[x].k>>a[x].next;//这样存储
}
int num=;
if(a[root].next==-){//没有一个在链表里
printf("%d %d",,-);
return ;
}
while(a[root].next!=-){//排序的节点必须都在链表里
b[++num].k=a[root].k;//换一种存储节点的方式,按顺序存储
b[num].add=root;
root=a[root].next;
}
b[++num].k=a[root].k;
b[num].add=root;
sort(b+,b++num,cmp);
printf("%d %05d\n",num,b[].add);
for(int i=;i<=num;i++){
printf("%05d %d ",b[i].add,b[i].k);
if(i!=num) printf("%05d\n",b[i+].add);
else cout<<"-1";
}
return ;
}

柳神的题解写的更好,学习一下:

分析:建立结构体数组,按照从首地址开始的顺序(直到-1)遍历一遍整个链表,将在链表中的结点的flag标记为true,并且统计cnt(有效结点的个数)。(因为有的结点根本不在链表中)
然后将链表进行排序,如果flag == false就把他们移动到后面(即:reuturn a.flag > b.flag),最后只输出前cnt个链表的信息~

#include <algorithm>
using namespace std;
struct NODE {
int address, key, next;
bool flag;
}node[];
int cmp1(NODE a, NODE b) {
return !a.flag || !b.flag ? a.flag > b.flag : a.key < b.key;
}
int main() {
int n, cnt = , s, a, b, c;
scanf("%d%d", &n, &s);
for(int i = ; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
node[a] = {a, b, c, false};
}
for(int i = s; i != -; i = node[i].next) {
node[i].flag = true;
cnt++;
}
if(cnt == ) {
printf("0 -1");
} else {
sort(node, node + , cmp1);
printf("%d %05d\n", cnt, node[].address);
for(int i = ; i < cnt; i++) {
printf("%05d %d ", node[i].address, node[i].key);
if(i != cnt - )
printf("%05d\n", node[i + ].address);
else
printf("-1\n");
}
}
return ;
}

PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)的更多相关文章

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

  2. PAT 甲级 1017 Queueing at Bank (25 分)(模拟题,有点思维小技巧,第二次做才理清思路)

    1017 Queueing at Bank (25 分)   Suppose a bank has K windows open for service. There is a yellow line ...

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

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

  4. PAT甲级1052 Linked List Sorting

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 题意: 给定一些内存中的节点的地址,值 ...

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

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

  6. 1052 Linked List Sorting (25分)

    题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...

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

    题意: 输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数).输出排序后的信 ...

  8. 【PAT甲级】1109 Group Photo (25分)(模拟)

    题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...

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

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

随机推荐

  1. 1204 中间件以及cookie,session

    目录 一 .cookie与session原理 1.cookie 操作 1.1 设置cookie set_cookie 1.2 获取cookie request.COOKIES.get('k1') 1. ...

  2. 12 复习 - webpack基本配置1

    1.npm包管理工具 npm init -y 如果创建的项目的根目录名称是中文或者包含中文,不能使用-y npm init 回车时要求你输入包的名称,自己手写项目名称,例test 2.新建src,di ...

  3. appium+python+iOS 环境搭建与使用中常见问题的解决方案链接

    (1)WebDriverAgent 安装入门篇:https://www.cnblogs.com/zhanggui/p/9239827.html 重点摘要: 在WDA的Github上也给出了WDA的特性 ...

  4. Linux 查看系统配置参数

    原文链接:http://www.cnblogs.com/aric2016/p/10971690.html 查看 cpu信息: cat /proc/cpuinfo 查看内存信息: grep MemTot ...

  5. MongoDB 如何保证 oplog 顺序?

    MongoDB 复制集里,主备节点间通过 oplog 来同步数据,Priamry 上写入数据时,会记录一条oplog,Secondary 从 Primary 节点拉取 oplog并重放,以保证最终存储 ...

  6. Luogu5540 最小乘积生成树

    Luogu5540 最小乘积生成树 题目链接:洛谷 题目描述:对于一个\(n\)个点\(m\)条边的无向连通图,每条边有两个边权\(a_i,b_i\),求使\((\sum a_i)\times (\s ...

  7. 1823:【00NOIP提高组】方格取数

    #include<bits/stdc++.h> using namespace std; ][]; ][][][]; inline int max(int x,int y) { retur ...

  8. 提高python运行效率的方法

    让关键代码依赖于外部包:你可以为紧急的任务使用C.C++或机器语言编写的外部包,这样可以提高应用程序的性能 使用生成器,因为可以节约大量内存 多个if elif条件判断,可以把最有可能先发生的条件放到 ...

  9. python2和python3区别

    字符编码: py3中默认字符编码是unicode:py2中默认字符编码是 ASCII,如果文件中出现了中文,需要在顶部加入coding声明#coding:utf8 让用户输入:py3中直接使用inpu ...

  10. Spring Cloud Gateway(四):路由定义定位器 RouteDefinitionLocator

    本文基于 spring cloud gateway 2.0.1 1.简介 RouteDefinitionLocator 是路由定义定位器的顶级接口,它的主要作用就是读取路由的配置信息(org.spri ...