PAT甲级1052、Linked LIst Sorting

ops/2025/2/6 9:44:49/

题目

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 (<10^5) 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 [−10^5,10^5], 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

思路

这个题和1032那个题差不多,都是用到了静态链表,可以总结一下,先说思路吧

明显要用到排序,sort函数比较一下就好了

关键是要重排链表,在输出中next这一部分和输入时是不一样的,幸好不是指针链表,不然只能强行冒泡了

关键点有两个:

1、题目中说的是“有n个节点在内存中”,并不一定有n个节点在链表中

2、n可以为0,又是这个玩意,以后还是默认positive包括0吧

#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
struct Node{int addr;int key;int next;bool flag;
}node[100005];bool cmp(Node a,Node b)
{if(a.flag == false || b.flag == false)return a.flag > b.flag;elsereturn a.key < b.key;
}
int main()
{int n,L;cin>>n>>L;for(int i=0;i<100005;i++)node[i].flag = false;for(int i=0;i<n;i++){int add,k,nex;cin>>add>>k>>nex;node[add].addr = add;node[add].key = k;node[add].next = nex;}int cnt = 0;for(int i=L;i!=-1;){node[i].flag = true;cnt++;i=node[i].next;}sort(node, node+100005, cmp);if(cnt)cout<<cnt<<" "<<setw(5)<<setfill('0')<<node[0].addr<<endl;elsecout<<"0 -1"<<endl;for(int i=0;i<cnt;i++){cout<<setw(5)<<setfill('0')<<node[i].addr<<" "<<node[i].key<<" ";if(node[i+1].flag == false)cout<<"-1"<<endl;elsecout<<setw(5)<<setfill('0')<<node[i+1].addr<<endl;}
}

总结

静态链表首先要求总的节点数量(地址)不能太大,这样才能定义一个大数组

然后要求地址不连续,甚至是相当稀疏的

最后是在指针链表不太方便做的时候可以用,比如说大量的交换节点顺序、查询比较信息这些频繁使用链表信息的操作

顺带补一句

在1032那个题中我感觉用静态链表是输入格式的问题,输入的地址是离散的,毫无顺序的,也就是说无法轻易地构建指针链表,所以一开始就分配好空间的静态链表比较合适

所以我感觉看输入格式可能是最有效的方法


http://www.ppmy.cn/ops/156118.html

相关文章

自定义数据集 使用pytorch框架实现逻辑回归并保存模型,然后保存模型后再加载模型进行预测,对预测结果计算精确度和召回率及F1分数

自定义数据集&#xff1a;继承 torch.utils.data.Dataset 类创建自定义数据集&#xff0c;并重写 __len__ 和 __getitem__ 方法。 定义逻辑回归模型&#xff1a;继承 nn.Module 类&#xff0c;定义一个线性层&#xff0c;并在 forward 方法中应用sigmoid激活函数。 训…

数据库系统概念第六版记录 一

1.关系型数据库 关系型数据库&#xff08;Relational Database&#xff0c;简称 RDB&#xff09;是基于关系模型的一种数据库&#xff0c;它通过表格的形式来组织和存储数据。每个表由若干行&#xff08;记录&#xff09;和列&#xff08;字段&#xff09;组成&#xff0c;数据…

移除元素-双指针(下标)

题目 给你一个数组 nums 和一个值 val&#xff0c;你需要 原地 移除所有数值等于 val 的元素。元素的顺序可能发生改变。然后返回 nums 中与 val 不同的元素的数量。 假设 nums 中不等于 val 的元素数量为 k&#xff0c;要通过此题&#xff0c;您需要执行以下操作&#xff1a…

Linux-Robust-Futex学习笔记

robust futex 简介 概述&#xff1a; 为了保证futex的robustness&#xff0c;添加了一种能够用于处理进程异常终止时锁状态的机制&#xff0c;就是当拥有锁的线程异常终止时&#xff0c;该线程能够将锁进行释放 基本原理&#xff1a; 每个线程都有一个私有的robust list&…

Spring Boot 实例解析:配置文件

SpringBoot 的热部署&#xff1a; Spring 为开发者提供了一个名为 spring-boot-devtools 的模块来使用 SpringBoot 应用支持热部署&#xff0c;提高开发者的效率&#xff0c;无需手动重启 SpringBoot 应用引入依赖&#xff1a; <dependency> <groupId>org.springfr…

深度学习之“线性代数”

线性代数在深度学习中是解决多维数学对象计算问题的核心工具。这些数学对象包括标量、向量、矩阵和张量&#xff0c;借助它们可以高效地对数据进行操作和建模。以下将详细介绍这些数学对象及其在深度学习中的典型用途。 数学对象概述 标量 标量是最简单的数学对象&#xff0…

算法 哈夫曼树和哈夫曼编码

目录 前言 一&#xff0c;二进制转码 二&#xff0c;哈夫曼编码和哈夫曼树 三&#xff0c;蓝桥杯 16 哈夫曼树 总结 前言 这个文章需要有一定的树的基础&#xff0c;没学过树的伙伴可以去看我博客树的文章 当我们要编码一个字符串转成二进制的时候&#xff0c;我们要怎么…

【回溯+剪枝】电话号码的字母组合 括号生成

文章目录 17. 电话号码的字母组合解题思路&#xff1a;回溯 哈希表22. 括号生成解题思路&#xff1a;回溯 剪枝 17. 电话号码的字母组合 17. 电话号码的字母组合 ​ 给定一个仅包含数字 2-9 的字符串&#xff0c;返回所有它能表示的字母组合。答案可以按 任意顺序 返回。 …