PAT A1052 Linked List Sorting
题意:
给出N个结点的地址address、数据域data以及指针域next,然后给出链表的首地址,要
求把在这个链表上的结点按data值从小到大输出。
样例解释:
按照输入,这条链表是这样的(结点格式为[address, data, next]):
[00001, 0, 22222]→[22222, 1000, 12345]→[12345, -1, 33333]→[33333, 100000, 11111]→[11111,100, -1]
按key值排序之后得到:
[12345, -1, 00001]→[00001, 0, 11111]→[11111, 100, 22222]→[22222, 1000, 33333]→[33333,100000, -11
此处可以直接套用前面讲解的一般解题步骤。
步骤1:定义静态链表,其中结点性质由bool型变量flag定义,表示为结点在链表中是
否出现。flag为false表示无效结点(不在链表上的结点)。
步骤2:初始化,令flag均为false(即0),表示初始状态下所有结点都是无效结点。
步骤3:由题目给出的链表首地址begin遍历整条链表,并标记有效结点的flag为true(即1),同时计数有效结点的个数count。
步骤4:对结点进行排序,排序函数cmp的排序原则是:如果cmp的两个参数结点中有无效结点的话。则 按flag大到小排序,以把有效结点排到数组左端(因为有效结点的flag为1,大于无效结点的flag),否则按数据域从小到大排序(第二级条件)。
步骤5:由于有效结点已经按照数据域从小到大排序,因此按要求输出有效结点即可.
注意点
1.可以直接使用 % 05d的输出格式,以在不足五位时在高位补0。但是要注意 - 1不能使用 % 05d输出,否则会输出 - 0001(而不是 - 1或者 - 00001),因此必须要留意 - 1的输出。
2.题目可能会有无效结点,即不在题目给出的首地址开始的链表上。
3.数据里面还有均为无效的情况,这时就要根据有效结点的个数特判输出”0 - 1”。
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100005;
struct Node {
int data;
int next;
int address;
bool flag;//判断节点是否在链表上。
}node[maxn];
bool cmp(Node a, Node b) {
if (a.flag == 0 || b.flag == 0) {
return a.flag > b.flag;
}
else {
return a.data < b.data;
}
}
int main() {
for (int i = 0; i < maxn; i++) {//初始化
node[i].flag = false;
}
int n, s, address;
scanf("%d%d", &n, &s);
for (int i = 0; i < n; i++) {
scanf("%d", &address);
scanf("%d%d", &node[address].data, &node[address].next);
node[address].address = address;
}
int count = 0,p = s;
while (p !=-1) {
node[p].flag = true;//进行标记
count++;//记录有效节点的个数。
p = node[p].next;
}
if (count == 0) {
printf("0 -1");
}
else {
sort(node, node + maxn, cmp);
printf("%d %05d\n", count, node[0].address);//先输出链表元素个数,再输出第一个节点的位置
for (int i = 0; i < count; i++) {
if (i != count - 1) {
printf("%05d %d %05d\n",node[i].address, node[i].data, node[i + 1].address);//第三个位置是下一个元素的地址
}
else {//如果是最后一个元素则将其下一个节点的位置输出为“-1”,防止-1被%05d化。
printf("%05d %d -1\n", node[i].address, node[i].data);
}
}
}
return 0;
}
PAT A1052 Linked List Sorting的更多相关文章
- PAT A1052 Linked List Sorting (25 分)——链表,排序
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- 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 ...
- PAT甲级——A1052 Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- A1052. Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We a ...
- PAT 1052. Linked List Sorting
这场考试当年还参加了,当时直接用内置的排序了,否则自己写归并排序浪费时间啊,现在来练习一发.估计又有些节点没在链表里面,当时没考虑这个情况,所以一直有些case没过 #include <iost ...
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- 【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 (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
随机推荐
- VAST维萨币二月发行,高倍币重现江湖!
市场长期的历史经验表明,经营盈利能力最好的企业,经常是那些现在的经营方式与5年前甚至10年前几乎完全相同的企业.这个经营模式放到币圈也是一样的,2020年的挖矿是最火的,这个模式现在在市场也同样受用. ...
- 新手如何通过SPC算力生态获得多重收益?
DeFi市场在去年的一波又一波热潮之后,在今年余温有些褪去.而资本市场也将目光从DeFi市场中转移开来,他们将目光对准了新的市场,即算力市场.算力,其实从区块链技术在大范围普及以来,就是一个常见的话题 ...
- 「NGK每日快讯」12.4日NGK公链第31期官方快讯!
- 国内源安装ROS2
资料参考: https://www.mscto.com/op/428870.html 需要修正的一点是,使用如下的命令添加源: sudo sh -c 'echo "deb [arch=$(d ...
- 远程过程调用框架——gRPC
gRPC是一款基于http协议的远程过程调用(RPC)框架.出自google.这个框架可以用来相对简单的完成如跨进程service这样的需求开发. 资料参考: https://blog.csdn.ne ...
- JavaScript高级:JavaScript面向对象,JavaScript内置对象,JavaScript BOM,JavaScript封装
知识点梳理 课堂讲义 1.JavaScript面向对象 1.1.面向对象介绍 在 Java 中我们学习过面向对象,核心思想是万物皆对象. 在 JavaScript 中同样也有面向对象.思想类似. 1. ...
- ElasticSearch DSL 查询
公号:码农充电站pro 主页:https://codeshellme.github.io DSL(Domain Specific Language)查询也叫做 Request Body 查询,它比 U ...
- scala:分别使用懒汉式和饿汉式实现单例模式
在java中,单例模式需要满足以下要求: 构造方法私有化,使得本类之外的地方不能使用构造方法new出对象 提供私有静态属性,接收单例对象 公共的.静态的getInstance方法,便于外界拿到单例对象 ...
- nignx的location正则匹配
原文链接:http://nginx.org/en/docs/http/ngx_http_core_module.html Syntax: location [ = | ~ | ~* | ^~ ] ur ...
- 操作系统---在内核中重新加载GDT和堆栈
摘要 用BIOS方式启动计算机后,BIOS先读取引导扇区,引导扇区再从外部存储设备中读取加载器,加载器读取内核.进入内核后,把加载器中建立的GDT复制到内核中. 这篇文章的最大价值也许在末尾,对C语言 ...