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. P2002 消息扩散[SCC缩点]

    题目描述 有n个城市,中间有单向道路连接,消息会沿着道路扩散,现在给出n个城市及其之间的道路,问至少需要在几个城市发布消息才能让这所有n个城市都得到消息. 输入格式 第一行两个整数n,m表示n个城市, ...

  2. P1078 文化之旅[最短路]

    题目背景 本题是错题,后来被证明没有靠谱的多项式复杂度的做法.测试数据非常的水,各种玄学做法都可以通过(比如反着扫),不代表算法正确.因此本题题目和数据仅供参考. 题目描述 有一位使者要游历各国,他每 ...

  3. python 实现定时任务

    需求: 想实现 每周一到周五下班钉钉打卡提醒,每周四发周报提醒 使用了二种方法实现 一:apscheduler,代码如下 import json,requests,datetime from apsc ...

  4. 个人项目———Java实现WordCount

    2018年系统分析与设计—个人项目作业 题目来自于 :https://edu.cnblogs.com/campus/xnsy/2018Systemanalysisanddesign/homework/ ...

  5. React 之 render props 的理解

    1.基本概念 在调用组件时,引入一个函数类型的 prop,这个 prop定义了组件的渲染方式. 2.回调渲染 回顾组件通信的几种方式 父-> 子 props 子-> 父 回调.消息通道 任 ...

  6. pid 及参数调试方法

    所谓PID指的是Proportion-Integral-Differential.翻译成中文是比例-积分-微分. 记住两句话: 1.PID是经典控制(使用年代久远) 2.PID是误差控制() 对直流电 ...

  7. man与info

    Linux系统中在线求助命令:man page 与info page 还有--help . --help没有man的详细,首先我们来看mna 命令.在linux中输入 man + 相关的文件 ,就可以 ...

  8. js的使用及语法

    通常,通过 JavaScript,您需要操作 HTML 元素. 1.通过 id 找到 HTML 元素 2.通过标签名找到 HTML 元素 3.通过类名找到 HTML 元素 提示:通过类名查找 HTML ...

  9. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  10. 2019暑期金华集训 Day1 组合计数

    自闭集训 Day1 组合计数 T1 \(n\le 10\):直接暴力枚举. \(n\le 32\):meet in the middle,如果左边选了\(x\),右边选了\(y\)(且\(x+y\le ...