当前位置: 首页 > article >正文

PAT甲级1052、Linked LIst Sorting

题目

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;
    else
        return 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;
    else
        cout<<"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;
        else
            cout<<setw(5)<<setfill('0')<<node[i+1].addr<<endl;
    }
}

总结

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

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

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

顺带补一句

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

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


http://www.kler.cn/a/534865.html

相关文章:

  • Javascript 日期计算如何实现当前日期加一天或者减去一天
  • java练习(8)
  • 《redis4.0 通信模块源码分析(一)》
  • DeepSeek R1技术报告关键解析(6/10):DeepSeek-R1 vs. OpenAI-o1-1217:性能对比分析
  • vue2-为啥data属性是一个函数而不是对象
  • 【数据分析】豆瓣电影Top250的数据分析与Web网页可视化(numpy+pandas+matplotlib+flask)
  • TongSearch3.0.4.0安装和使用指引(by lqw)
  • python处理json文件
  • 人工智能丨PyTorch 计算机视觉
  • [创业之路-286]:《产品开发管理-方法.流程.工具 》-1- IPD两个跨职能团队的组织
  • (安全防御)防火墙安全策略部署
  • 玩转Amazon Bedrock基础模型:解锁图像风格混搭的无限可能
  • 【论文复现】基于适应度-距离平衡的自适应引导差分进化算法用于考虑可再生能源的安全约束最优潮流问题
  • 【Go语言快速上手】第一部分:Go 语言基础
  • Angular-hello world
  • 青少年编程与数学 02-008 Pyhon语言编程基础 22课题、类的定义和使用
  • C++【深入 STL--list 之 迭代器与反向迭代器】
  • 【鸿蒙HarmonyOS Next实战开发】视频压缩库VideoCompressor
  • Vue混入(Mixins)与插件开发深度解析
  • 常用抓包工具tcpdump、Fiddler、Charles、Wireshark 和 Sniffmaster 下载地址
  • 使用 CMake 自动管理 C/C++ 项目
  • C语言程序设计P6-5【应用指针进行程序设计 | 第五节】——指针与函数
  • OCR--光学字符识别
  • WebSocket推送数据快,条数多导致前端卡顿问题解决
  • 《Linux基础优化与常用软件包》
  • 【大数据技术】词频统计样例(hadoop+mapreduce+yarn)