静态链表(用结构体数组模拟链表)
 
 
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. Effective.Java第1-11条

    1. 考虑使用静态工厂方法替代构造方法 一个类可以提供一个公共静态工厂方法,它只是返回类实例的静态方法.例如JDK的Boolean的valueOf方法: public final class Bool ...

  2. c++ 二维数组定义 二维数组首地址查询

    #include <iostream> using namespace std; int main() { ][] = { {,,}, {,,} }; cout << &quo ...

  3. golang---命令源码文件与命令行参数

    命令源码文件是程序的运行入口,是每个可独立运行的程序必须拥有的. import "flag"  flag包实现了命令行参数的解析.每个参数认为一条记录,根据实际进行定义,到一个se ...

  4. Python开发【第十四篇】装饰器

    装饰器 什么是装饰器? ​ 装饰器是一个函数,主要作用是用来给包装另一个函数或者类 包装的目的是不改变原函数名(或类名)的情况下改变或添加被包装对象的功能 函数装饰器 是指装饰器是一个函数,传入的是一 ...

  5. C# MemcacheHelper封装

    using Memcached.ClientLibrary; using System; using System.Collections.Generic; using System.Linq; us ...

  6. ASP.NET SignalR 系列(七)之服务端触发推送

    前面几章讲的都是从客户端触发信息推送的,但在实际项目中,很多信息可能是由系统服务端推送的,下面2图分别展示两种通道 客户端触发推送 服务端推送 下面我们就重点介绍下服务端如何调用集线器的对象进行推送 ...

  7. Mybatis映射器接口代理对象的方式 运行过程

    查询一张表的所有数据. 环境: 使用工具IntelliJ IDEA 2018.2版本. 创建Maven工程不用骨架 1.pom.xml <?xml version="1.0" ...

  8. Django:RestFramework之-------频率控制

    5.频率控制 5.1频率监控代码实现 import time VISIT_RECORD = {} class VisitThrottle(object): def __init__(self): #定 ...

  9. 通过nginx部署前端代码实现前后端分离

    实现前后端分离,可以让前后端独立开发.独立部署.独立单测,双方通过JSON进行数据交互. 对于前端开发人员来说,不用每次调试都需要启动或配置Java/Tomcat运行环境:对于后端开发人员来说 ,也不 ...

  10. Android Scroller解析

    作用 这个类封装了滚动操作,如帮我们处理手指抬起来时候的滑动操作.与ViewGroup的scrollTo(),scrollBy()的生硬式移动,Scroller提供了一个更加柔和的移动效果.Scrol ...