题意:
给出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的更多相关文章

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

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

  2. PAT 1052 Linked List Sorting [一般]

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

  3. Pat 1052 Linked List Sorting (25)

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

  4. PAT甲级——A1052 Linked List Sorting

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

  5. A1052. Linked List Sorting

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

  6. PAT 1052. Linked List Sorting

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

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

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

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

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

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

随机推荐

  1. Baccarat项目专用代币BGV的价值如何?

    NGK投资者对于NGK平台自身的DeFi项目呼声越来越高,经过数月的紧张研发,检验和内测工作,NGK官方将于近日推出其去中心化金融项目--Baccarat,此项目为避免以太坊易被攻击,网络拥堵出块慢以 ...

  2. 两年Java,去字节跳动写Python和Go

    前言 2019年5月,在收到offer邮件的那一刻,我仍然不敢相信自己这一番际遇.经历了七场面试,终于得偿所望,拿到了字节跳动的offer. 做加入大厂的决定并不是巧合.在多年的职业生涯里,我曾多次对 ...

  3. 将VMware虚拟机最小化到托盘栏

    版权:本文采用「署名-非商业性使用-相同方式共享 4.0 国际」知识共享许可协议进行许可.   目录 前言 将VMware最小化到托盘栏的方法 1.下载 Trayconizer 2.解压 trayco ...

  4. nacos配置中心之服务器端

    配置信息的发布 配置信息发布请求URL: POST: /v1/cs/configs nacos在STANDALONE模式或集群模式没有指定用mysql情况下使用derby数据库,在集群模式且指定mys ...

  5. 《Linux学习笔记:文本编辑最佳实践》

    [Linux文本编辑的四种方法] 例如,要想test.txt文件添加内容"I am a boy",test.txt在当前目录中 方法一:vi编辑法 [推荐] 打开终端,输入vi t ...

  6. Synchronized 轻量级锁会自旋?好像并不是这样的。

    本来是在写面霸系列的,写着写着就写到了这一题: Synchronized 原理知道不? 而关于 Synchronized 我去年还专门翻阅 JVM HotSpot 1.8 的源码来研究了一波,那时候我 ...

  7. LanQiao-297(快速排序)

    快速排序 LanQiao-297 #include<iostream> #include<cstdio> #include<algorithm> #include& ...

  8. POJ-1459(最大流+EK算法)

    Power Network POJ-1459 这题值得思索的就是特殊的输入,如何输入一连串字符.这里采用的方法是根据输入已知的输入格式,事先预定好要接受的数据类型. 这里套用的板子也是最大流的模板,但 ...

  9. 基于es实现一个简单的搜索引擎

    一.什么是es Elasticsearch是一个基于ApacheLucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功能最全的搜索引擎库.但 ...

  10. NoSQL安装与操作

    redis操作: redis的启动与关闭:注意:(需要关闭防火墙) redis的启动:redis-server redis.conf redis的登录:redis-cli -a pass redis的 ...