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 ...
随机推荐
- 由一个问题引起的思考:WEB开发中,使用JSON-RPC好,还是RESTful API好?
起因: 研究zabbix的API设计风格.查看zabbix官网API文档,可以看到使用的是json-rpc:2.0 随后搜索到知乎上的一个问题讨论:https://www.zhihu.com/ques ...
- GO中标识符,标识符,变量,常量,iota
参考:https://www.cnblogs.com/nickchen121/p/11517455.html 一.标识符与关键字 1.标识符 在编程语言中标识符就是程序员定义的具有特殊意义的词,比如变 ...
- 【简记】修改Docker数据目录位置,包含镜像位置
为啥要改? Docker安装后默认下载的位置在/var/lib/docker ,如果/var分区没有独立分出来,Linux下默认是与/根分区在一起.一般我们装Linux系统的时候,除了做邮件服务器外, ...
- sql 语句中定义的变量不能和 sql关键字冲突
sql 语句中定义的变量不能和 sql关键字冲突 from bs_email_account account LEFT JOIN bs_group_info gp ON account.group_i ...
- 《PHP7底层设计与源码实现》学习笔记1——PHP7的新特性和源码结构
<PHP7底层设计与源码实现>一书的作者陈雷亲自给我们授课,大佬现身!但也因此深感自己基础薄弱,遂买了此书.希望看完这本书后,能让我对PHP7底层的认识更上一层楼.好了,言归正传,本书共1 ...
- Java的表达式和运算符
一.算术运算符 运算符 + - * / % 说明 加 减 乘 除 取模(余数) 例子 1+2 5-3 20*5 6/4 30%9 结果 3 2 100 1 3 int x = 10; int y = ...
- java中什么是抽象类(abstract)
一.什么是抽象类 由abstract修饰的方法叫抽象方法:由abstract修饰的类叫抽象类.抽象的类无法进行实例化,因为他不是具体存在的类,或者说这样的类还不够完善,不能直接使用new关键字调用其构 ...
- 前端工程师拿到全新的 Mac 需要做哪些准备
最近苹果退出了新款 Mac,用了3年15款Pro之后,终于盼到18款的降价,于是含泪更新换代 但是每次换电脑,重装环境的好多东西记不清,于是记个笔记 一.终端 安装 zsh sh -c "$ ...
- VirtualDub在处理WMV文件时显示“MISSING CODEC”怎么办
以下内容主要来自:http://www.brilliantcode.com/virtualdub-is-showing-missing-codec-when-i-play-a-wmv-movie-ev ...
- Linux基础:时间同步工具Chrony
在Linux下,默认情况下,系统时间和硬件时间,并不会自动同步.在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰.硬件时间的运行,是靠Bios电池来维持,而系统时间,是用CPU t ...