静态链表(用结构体数组模拟链表)
 
 
1052 Linked List Sorting (25分)
 

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的更多相关文章

  1. PAT 解题报告 1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) A linked list consists of a series of structures, which are not neces ...

  2. Linked List Sorting (链表)

    Linked List Sorting (链表)   A linked list consists of a series of structures, which are not necessari ...

  3. PAT1052:Linked List Sorting

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  4. 【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 ...

  5. PAT 1052 Linked List Sorting [一般]

    1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...

  6. Pat 1052 Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  7. pat1052. Linked List Sorting (25)

    1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...

  8. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  9. 1052. Linked List Sorting (25)

    题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...

随机推荐

  1. Java核心技术-读书笔记

    基本语法 Java中的所有函数都属于某个类的方法 Java没有任何无符号的int.long.short 或 byte 类型 浮点数值不适用于无法接受舍入误差的金融计算中,比如2.0-1.1不会输出想要 ...

  2. 乘法器——基于Wallace树的4位乘法器实现

    博主最近在学习加法器乘法等等相关知识,在学习乘法器booth编码加Wallace树压缩时,发现在压缩部分积的时候用到了进位保留加法器(Carry Save Adder),博主对这种加法器不是很理解,而 ...

  3. Codeforces Round #580 (Div. 1)

    Codeforces Round #580 (Div. 1) https://codeforces.com/contest/1205 A. Almost Equal 随便构造一下吧...太水了不说了, ...

  4. SpringBoot引入第三方jar包或本地jar包的处理方式

    在开发过程中有时会用到maven仓库里没有的jar包或者本地的jar包,这时没办法通过pom直接引入,那么该怎么解决呢 一般有两种方法 第一种是将本地jar包安装在本地maven库 第二种是将本地ja ...

  5. Delphi中HInstance

    通过测试看出:HInstance.Application.Handle.Self.Handle不是一回事. Self.Handle是窗体句柄: Application.Handle也是个窗体的句柄,不 ...

  6. vue日历/日程提醒/html5本地缓存

    先上图 功能: 1.上拉日历折叠,展示周 2.左右滑动切换月 2.“今天”回到今天:“+”添加日程 3.localStorage存储日程 index,html <body> <div ...

  7. wps金山文档在线编辑--.Net 接入指南

    一.申请成为服务商,对金山文档在线服务进行申请 ①进入官网 https://open.wps.cn/ ②申请后如下图,点击右下角的进入服务 ③申请成功后 ④数据回调URL一定是服务器地址,本次我使用的 ...

  8. windows下vmware和Hyper-v共存方法

    问题描述:环境:windows server 2012 r2系统下安装Hyper-v后,再安装Vmware 在Vmware中创建虚拟机,安装虚拟机系统的时候,vmware提示:VMware Works ...

  9. rest-spring-boot-starter

    rest-spring-boot-starter 基于spring boot,统一业务异常处理,统一返回格式包装 依赖 <dependency> <groupId>tk.fis ...

  10. 【JVM】G1垃圾收集器深入分析

    一.和CMS对比   G1 CMS 设计原则 首先收集尽可能多的垃圾(Garbage First) 尽可能少而块地执行GC,以停顿时间为目标 垃圾回收时机  启发式算法,在老年代找出具有高收集收益的分 ...