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. 2020年日期表-python实现

    import pandas as pdimport calendarimport datetime # 生成日期范围date = pd.date_range("2020-01-01" ...

  2. java中使用redis --- Set集合的简单应用

    1.java代码 public class RedisTest01 { public static void main(String[] args){ // connect redis server ...

  3. flask实现文件下载功能

    在Flask想要实现文件下载功能,只需要导入 send_from_directory(directory, filename, **options) 然后在视图函数中返回该函数即可 示例代码如下 fr ...

  4. 使用tippecanoe把GeoJSON制作成供mapbox展示的矢量切片vectortile

    本文记录一下把geojson格式的数据制作成本地的矢量切片,并在mapbox中展示的过程. 1.切片 1.1 矢量数据需要先转换为geojson,如果是shp格式可以使用QGIS或者下载shp2gwo ...

  5. 搭建gitlab服务

    安装依赖 sudo yum install curl policycoreutils openssh-server openssh-clients sudo systemctl enable sshd ...

  6. idea2018使用整理

    1.idea怎么设置选中文件时,自动在左侧弹出文件所在位置及文件?

  7. 差分约束 4416 FFF 团卧底的后宫

    /* 4416 FFF 团卧底的后宫  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 你在某日收到了 FFF ...

  8. windows server2012 搭建.netcore+nginx+nssm运行环境

    1.linux+.netcore+sqlserver的坑 linux不支持访问sqlserver2008及以下的版本(由于System.Data.SqlClient.dll的限制.windows上面访 ...

  9. [Go] 基础系列二:channel的关闭和广播

    利用channe关闭任务 package ch21 import ( "fmt" "testing" "time" ) //判断是否有取消任 ...

  10. 将页面中所有的checkbox设成单选得

    $(function () { var allBox = $(":checkbox"); allBox.click(function () { allBox.removeAttr( ...