静态链表(用结构体数组模拟链表)
 
 
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. 【数据结构与算法】单链表操作(C++)

    #include <stdio.h> #include <malloc.h> /*单链表节点定义*/ typedef struct LNode { int data; //da ...

  2. Java学习:final关键字的使用与注意事项

    final 关键字代表最终.不可改变的. 常见的四种用法 可以用来修饰一个类 可以用来修饰一个方法 可以用来修饰一个局部变量 可以用来修饰一个成员变量 1.当final关键字用来修饰一个类的时候,格式 ...

  3. 计算标准差——Python

    计算标准差 题目描述: 编写一个函数计算一系列数的标准差.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪ ...

  4. java中String字符串

    一.定义String字符串 String字符串和char字符不同,char使用单引号,只能表示一个字符,字符串就是一段文本.String是个类.这个类使用final修饰,所以这个类是不可以继承扩充和修 ...

  5. C#文件操作之把字符串取到文本文件及把文本文件读取到字符串中

    一.把字符串读取到文本文件中 using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate))//把json读到一个文本中 { S ...

  6. ios app UI自动化测试用到的命令

    ios测试的app测试包,真机设备需要开发者证书并且将测试机的udid加入到pp文件文件,configruation 要求为debug模式的ipa包, 1.苹果手机的UDID, a.通过 xcode- ...

  7. MybatisGenerator生成SSM的dao层

    官网下载 mybatis generator 下载generator的release版本mybatis-generator-core-1.4.0-bundle.zip https://github.c ...

  8. AR自动开票主程序导入发票的时候,出现错误提示''不能获取汇款地址''

    问题:AR自动开票主程序,出现错误不能获取汇款地址 解决:AR>设置-打印-汇入地址,汇入地址要增加此客户地点对应的国家:

  9. Linux中Nginx中添加自签证书TLS

    创建自签证书TLS openssl req \ -newkey rsa: \ -x509 \ -nodes \ -keyout test.com.key \ -new \ -out test.com. ...

  10. WinForm背景图片及图片位置

    设置背景图片:BackgroundImage属性选择对应的图片就可以了. 背景图片随窗体的变化而变化:BackgroundImageLayout属性值设置为Stretch. 窗体放置图片:Pictur ...