题目描述

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 −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 [], 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

解题思路

  • 静态链表->结构体数组->清除无效节点顺便数组化->对数组进行排序

  • 此时关键字key是有序的

  • 但需要更新一下next关系

  • 输出结构体数组

  • 结构体数组化的方式:vector<node> Array

注意

  • 如果不进行数组化,那么结构体的是不能进行移动位置的,也就不能进行排序了

  • 需要对存储静态链表的结构体数组进行清除无效节点,并重新计算节点数量newN

  • 边界条件:节点全部无效的时候,需要输出0 -1

静态链表存储结构

注意包含自己的地址,这样很方便更新next

结构体定义

struct node{int addr;//存储自己地址,会很方便的更新nextint key;int next;
}NodeList[100005] ;

更新next

//  更新next关系(当然了,不更新也行,直接输出这种)for(int i=0;i<newN-1;i++){Array[i].next = Array[i+1].addr;//存储自己地址,会很方便的更新next}

收获与思考

我们来理一下对于静态链表题目的思路

  1. 用结构体存储,结构体定义要完整,见上面

  1. 进行清洗,删除无效节点,与此同时数组化,数组化的方式见上面

  1. 如果需要更改顺序,别忘记更新next

代码

#include <iostream>
#include <vector>
#include<algorithm>
#include <iterator>
using namespace std;struct node{int addr;//存储自己地址,会很方便的更新nextint key;int next;
}NodeList[100005] ;
bool cmp(node a,node b)
{return a.key<b.key;
}
vector<node> Array;
int main()
{int N,head;scanf("%d %d",&N,&head);int addr,key,next;
// 注意有一个问题,有没有可能有无效节点,这是需要去除的!for(int i=0;i<N;i++){scanf("%d %d %d",&addr,&key,&next);NodeList[addr].addr = addr;NodeList[addr].key = key;NodeList[addr].next = next;}
//  对无效节点进行清除并数组化int newN = 0;while(head!=-1){Array.push_back(NodeList[head]);head = NodeList[head].next;newN++;}
//  对结构体进行排序sort(Array.begin(),Array.end(),cmp);
//  如果全都无效(这一点很边界),那么只能输出 0 -1if(newN==0){printf( "0 -1");return 0;}//  更新next关系(当然了,不更新也行,直接输出这种)for(int i=0;i<newN-1;i++){Array[i].next = Array[i+1].addr;//存储自己地址,会很方便的更新next}Array[newN-1].next = -1;printf("%d %05d\n",newN,Array[0].addr);for(int i=0;i<newN-1;i++){printf("%05d %d %05d\n",Array[i].addr,Array[i].key,Array[i].next);}printf("%05d %d %d\n",Array[newN-1].addr,Array[newN-1].key,Array[newN-1].next);return 0;
}

1052 Linked List Sorting 分数 25相关推荐

  1. 1052 Linked List Sorting (25 分)

    Sample Input: 5 00001 11111 100 -1 00001 0 22222 33333 100000 11111 12345 -1 33333 22222 1000 12345 ...

  2. PAT甲级1052 Linked List Sorting:[C++题解]链表排序

    文章目录 题目分析 题目链接 题目分析 题意:给定数据(里面有不构成链表的数据,若是,则跳过),是链表的构成链表.然后根据数值大小重新排序,构成新的链表. 分析:用数组模拟链表,先建立链表.遍历链表, ...

  3. 1052. Linked List Sorting

    没仔细审题,走了很多弯路.题目要求:内存中有很多散列的节点,可能构成不止一个链表.其次要注意空链表的情况. // 1052. Linked List Sorting.cpp: 主项目文件.#inclu ...

  4. 1052 Linked List Sorting(排序)

    1052 Linked List Sorting(排序) 思路: s t r u c t + struct+ struct+排序. 坑点: 1.答案只包含从原链表中头结点开始的结点,有点结点不在原链表 ...

  5. 【附段错误原因,最后两个测试点】1052 Linked List Sorting (25 分)【链表类题目总结】

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A linked list consists of a series of structures, which are not n ...

  6. 1052. Linked List Sorting (25)

    考察链表的知识,以及排序 题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1052 #include<iostream> #inclu ...

  7. 1052 Linked List Sorting (25 分)【难度: 一般 / 知识点: 链表】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805425780670464 哈希表模拟链表. #include<b ...

  8. 1052 Linked List Sorting (25分)

    和 1074一样的方法,只需要管自己的结点就可以了,不需要处理next, 注意最后一个测试点,测试数据最终结果 为0个结点,只需要输出 0 -1 附本人AC代码: #include<iostre ...

  9. 1052 Linked List Sorting

    1. 开始测试点4不通过,得分24/25,是忽略了所有节点都不在链表上的特殊情况. 2. 其实就是用静态链表,把结点根据值的大小,升序排列,所以一开始把每个结点的key赋值为超出最大值的maxn,是为 ...

最新文章

  1. 下一步,该怎么做空中国概念股?
  2. Android学习笔记(五)——数据存储(二)SQLite和ContentProvider
  3. GlobalPointer:用统一的方式处理嵌套和非嵌套NER
  4. 「SVN」Linux下svn命令使用的实践,个人记录~=傻瓜教程
  5. 抽象类可以创建对象吗_【基础篇】java-抽象类与继承的补充
  6. BZOJ 2288: 【POJ Challenge】生日礼物 优先队列+贪心+链表
  7. 云计算设计模式(二十四)——仆人键模式
  8. VB6之从1970年1月1日起的秒数 的与C语言类似的时间函数
  9. c语言dda算法完整实现,计算机图形学DDA算法.doc
  10. Redis入门——狂神课程笔记
  11. 谈谈德国大学的电子专业
  12. 5s的app显示无法连接服务器,苹果手机无法连接到app store怎么办
  13. python离线安装第三方库
  14. ACM各OJ网站简介
  15. 2023年江苏省赛事网络空间安全理论题库
  16. spark笔记spark优化
  17. 谷歌浏览器访问网站提示“您要访问的网站包含恶意软件”
  18. 程序yuan开发-windows小工具:gif截图-LICEcap、文件查找-Everything、远程软件-TeamView、视频播放-PotPlayer、markdown编辑-Typora、
  19. 日常中一些好用的小软件
  20. api接口设计相关总结

热门文章

  1. yaml文件编写格式
  2. Himall商城信任登录用户信息
  3. 苹果手机群控免越狱手机投屏
  4. Linux 磁盘划分规则,如何规范的划分,比如300G, 900G,1T, 2T都是如何划分比较好。
  5. 如何选择补丁管理工具
  6. Java学习第十七天--Map
  7. Linux下安装Scrapy框架
  8. 微信小程序跳转外部网站
  9. 随机算法——蒙特卡罗算法——模式匹配问题
  10. 鼠标事件中MouseDown、MouseUp与Click事件有什么区别?