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 (<) 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
采用结构体数组模拟链表,用结构体中的一个变量标记该元素是否出现在链表上,采用二级排序,按照值是否有效,以及数值从小到大排序,所有有效的数据均会出现在数组的左边
一开始从st开始遍历链表获得flag和cnt等信息
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std ; const int N = ; struct node{
int adr,data,next ;
bool flag ;
}arr[N]; bool cmp(node &p,node &q){
if(!p.flag || !q.flag){
return p.flag > q.flag ;
}else{
return p.data < q.data ;
}
} int main(){
int n, st ;
cin >> n >> st ;
for(int i=;i<n;i++){
int a,b,c ;
cin >> a >> b >> c ;
arr[a].adr = a ;
arr[a].data = b ;
arr[a].next = c ;
arr[a].flag = false ;
} int cnt = ,b = st ;
while(b != -){
cnt ++ ;
arr[b].flag = true ;
b = arr[b].next ;
} sort(arr,arr+N,cmp) ; if(!cnt){
cout << "0 -1" << endl ;
}else{
printf("%d %05d\n",cnt,arr[].adr) ;
for(int i=;i<cnt;i++){
if(i < cnt -){
printf("%05d %d %05d\n",arr[i].adr,arr[i].data,arr[i+].adr) ;
}else{
printf("%05d %d -1\n",arr[i].adr,arr[i].data) ;
}
}
} return ;
}
...
Linked List Sorting的更多相关文章
- PAT 解题报告 1052. Linked List Sorting (25)
1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...
- Linked List Sorting (链表)
Linked List Sorting (链表) A linked list consists of a series of structures, which are not necessari ...
- PAT1052:Linked List Sorting
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- 【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 ...
- PAT 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- pat1052. Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
随机推荐
- mysql启动报错 "unknown variable 'defaults-file=/etc/my.cnf"
使用指定的my.cnf,而不用默认的/etc/my.cnf文件,可以在启动时,在mysqld_safe后加上参数--default-file=/usr/local/server/mysql2/etc/ ...
- ElasticSearch 线程池类型分析之 ResizableBlockingQueue
ElasticSearch 线程池类型分析之 ResizableBlockingQueue 在上一篇文章 ElasticSearch 线程池类型分析之 ExecutorScalingQueue的末尾, ...
- 【数据结构与算法】线性表操作(C++)
#include <stdio.h> #define maxSize 100 //定义整型常量maxSize值为100 /*顺序表的结构体定义*/ typedef struct SqLis ...
- Flink 源码解析 —— 项目结构一览
Flink 源码项目结构一览 https://t.zsxq.com/MNfAYne 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac ...
- Delphi微信支付【支持MD5和HMAC-SHA256签名与验签】
作者QQ:(648437169) 点击下载➨微信支付 微信支付api文档 [Delphi 微信支付]支持付款码支付.二维码支付.订单查询.申请退款.退款查询.撤销订单.关闭订单. ...
- FPGA控制RGMII接口PHY芯片基础
一.前言 网络通信中的PHY芯片接口种类有很多,之前接触过GMII接口的PHY芯片RTL8211EG.但GMII接口数量较多,本文使用RGMII接口的88E1512搭建网络通信系统.这类接口总线位宽小 ...
- JDK9对集合添加的优化
JDK9对集合添加的优化 JDK9的新特性: list接口,Set接口,Map接口:里边增加了一个静态的方法of,可以给集合一次性添加多个元素 static <E> List<E&g ...
- java中什么是包
一.什么是包 包允许将类组合成较小的单元(类似文件夹),使其易于找到和使用相应的类文件 包有助于避免命名冲突.在使用许多类时,类和方法的名称很难决定.有时需要使用与其他类相同的名称.包基本上隐藏了类并 ...
- 排序算法Java代码实现(一)—— 选择排序
以下几篇随笔都是记录的我实现八大排序的代码,主要是贴出代码吧,讲解什么的都没有,主要是为了方便我自己复习,哈哈,如果看不明白,也不要说我坑哦! 本片分为两部分代码: 常用方法封装 排序算法里需要频繁使 ...
- rsyslog详解实战和避坑
目标是要把线上环境的debug日志及集中化收集起来,一方面是方便开发调试:一方面是避免直接到线上环境查看,存在安全隐患. 常用可选方案: rsyslog发送端 + rsyslog接收端: 直接存在接收 ...