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

PAT A 1052 Linked List Sorting

A linked list consists of a series of structures, which are not nec‐essarily adjacent in memory. We assume that each structure con‐tains 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 () and an address of the head node, where is the total number of nodes in memory and the ad‐dress of a node is a 5-digit positive integer. NULL is represented by .
Then 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 in‐teger 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 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

#include <iostream>
#include <algorithm>
#include <cstring>using namespace std;int n,head;
const int N = 1e5+10;
struct Node
{int add;int data;int next;bool flag;bool exist;//为了防止全部无效的情况bool operator <(const Node &t) const{if(flag != t.flag) return flag > t.flag;else return data < t.data;}
}node[N];int main()
{cin>>n>>head;int add = 0,next = 0,data = 0;for(int i=0;i<n;i++){cin>>add>>data>>next;node[add].add = add;node[add].next = next;node[add].data = data;node[add].exist = true;//至少是个节点}int cnt = 0;for(int i=head;i!=-1;i=node[i].next){if(node[i].exist == true)//为了杜绝头指针无next{node[i].flag = true;cnt++;}else break;}sort(node,node+N);if(cnt == 0) cout<<"0 -1"<<endl;//没有节点在链上else{cout<<cnt<<' ';printf("%05d\n",node[0].add);for(int i=0;i<cnt;i++){if(i<cnt-1){printf("%05d %d %05d\n",node[i].add,node[i].data,node[i+1].add);}else{printf("%05d %d ",node[i].add,node[i].data);printf("-1\n");}}}return 0;
}
http://www.lryc.cn/news/575349.html

相关文章:

  • 解决uniapp vue3版本封装组件后:deep()样式穿透不生效的问题
  • ZYNQ GP总线深度实战:智能灯光控制器的PS-PL交互艺术
  • Python 惰性求值实战:用生成器重构 Sentence 类
  • 从HTML4到HTML5+CSS3,如何快速掌握?(有老版HTML基础或经验)
  • Web基础关键_001_HTML(一)
  • QTextEdit、QTextBrowser右键菜单汉化显示
  • 数据结构大项目
  • 科技与人类贪欲
  • 医疗AI专科子模型联邦集成编程分析
  • 图像质量对比感悟
  • 【RESTful接口设计规范全解析】URL路径设计 + 动词名词区分 + 状态码 + 返回值结构 + 最佳实践 + 新手常见误区汇总
  • 2D 基准情况下贝叶斯优化应用的概率推理
  • centos 7 安装NVIDIA Container Toolkit
  • 云原生 Cloud Native
  • OBCP第三章 OceanBase SQL 引擎高级技术学习笔记
  • Rust 中的 HTTP 请求利器:reqwest
  • 【STM32】端口复用和重映射
  • 一次性登录令牌(Login Ticket)生成机制分析
  • 环境太多?不好管理怎么办?TakMll 工具帮你快速切换和管理多语言、多版本情况下的版本切换。
  • 【Actix Web】Rust Web开发实战:Actix Web框架全面指南
  • 从零到一训练一个 0.6B 的 MoE 大语言模型
  • 百面Bert
  • 《网络攻防技术》《数据分析与挖掘》《网络体系结构与安全防护》这三个研究领域就业如何?
  • ASP.NET Core Web API 实现 JWT 身份验证
  • list类的详细讲解
  • 基于 Python 的批量文件重命名软件设计与实现
  • 二叉树理论基础
  • 【偏微分方程】基本概念
  • 逆向入门(8)汇编篇-rol指令的学习
  • 【kubernetes】--Service