题目

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

题目分析

已知N个节点,将节点按照值排序(升)

解题思路

  1. 定义结构体数组,保存所有结点信息,节点中定义标记属性flag(初始化为false)
  2. 遍历所有结点信息,将出现的节点flag标记为true,并定义计数器统计链表中结点数count
  3. 遍历链表结点,修改next值为下一个结点

易错点

  1. 题目中不容易分析出结点中有无效结点
  2. 地址格式必须为"%05d"
  3. 若结点数为0,打印"0 -1"

知识点

  1. 将无效结点排序到最后的办法

    !a.flag || !b.flag ? a.flag > b.flag//降序true=1在前,false=0在后

Code

Code 01

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

PAT Advanced 1052 Linked List Sorting (25) [链表]的更多相关文章

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

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

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

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

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

  4. Pat 1052 Linked List Sorting (25)

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

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

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

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

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

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

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

  8. 1052. Linked List Sorting (25)

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

  9. PAT甲级1052 Linked List Sorting

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

随机推荐

  1. 吴裕雄--天生自然C++语言学习笔记:C++ 数组

    C++ 支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比如 number0. ...

  2. Node.js的启动和调试方式

    通过node命令启动 node server/bin/www webstorm配置启动入口 pm2 全局安装:cnpm i pm2 -g 检查版本:pm2 -v 启动:cd 项目目录 pm2 star ...

  3. Vue 改变数组触发视图更新

    最近给table做了一个点击排序的功能,数组更改以后发现data数据变了,但是视图不更新 写惯了js的我们随手一串代码 this.items[2]={message:"Change Test ...

  4. CodeForces - 402B Trees in a Row (暴力)

    题意:给定n个数,要求修改其中最少的数,使得这n个数满足ai + 1 - ai = k. 分析: 暴力,1000*1000. 1.这n个数,就是一个首项为a1,公差为k的等差数列.k已知,如果确定了a ...

  5. 20 ~ express ~ 前台内容分页展示

    一,后台路由文件 /router/main.js var express = require('express') var router = express.Router() var Category ...

  6. idea导入新项目后右键main方法没有Run xxx.main()

    刚安装的idea2019.1,基本上没有什么配置.导入从github clone下来的工程.鼠标右键main方法时没有发现 run 选项,如下图所示: 这里是配置有问题造成的,对比下正常的工程,可以发 ...

  7. 关于javascript中this 指向的4种调用模式

    this指向问题绝对可以排js 的top 5最难和最重点的问题,初学者常常搞不清楚this指向哪里,特别是学过java和c#的人,想当年俺也迷糊了好久,直到遇到蝴蝶书,主要是因为js和主流的面向对象语 ...

  8. Python 操作csv和excel表格

    1. 操作csv表格 使用的库 csv 1. csv文件里读取数据 代码 1. 以列表形式打开 import csv f = open('csv_test.csv', 'r') # 打开csv文件 c ...

  9. promise 核心技术3 使用

    什么是promise?(加深理解) 抽象表达:(比较高的高度 看这门技术) Promise是js中进行异步操作的新的解决方案(旧形式:纯回调的形式) 具体表达: 从语法上,Promise是一个构造函数 ...

  10. HDU 5423:Rikka with Tree Dijkstra算法

    Rikka with Tree  Accepts: 207  Submissions: 815  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...