PAT A1052 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 (<105) 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 −1.
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 [−105,105], 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
#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = ;
struct node{
int add,data,next;
}nodes[maxn];
bool cmp(node n1,node n2){
return n1.data<n2.data;
}
int main(){
vector<node> v;
int n,st;
scanf("%d %d",&n,&st);
for(int i=;i<n;i++){
int r,v,ne;
scanf("%d %d %d",&r,&v,&ne);
nodes[r].add = r;
nodes[r].data = v;
nodes[r].next = ne;
}
while(st!=-){
v.push_back(nodes[st]);
st = nodes[st].next;
}
sort(v.begin(),v.end(),cmp);
if(v.size()==){
printf("0 -1\n");
return ;
}
printf("%d %05d\n",v.size(),v[].add);
for(int i=;i<v.size()-;i++){
printf("%05d %d %05d\n",v[i].add,v[i].data,v[i+].add);
}
printf("%05d %d -1\n",v[v.size()-].add,v[v.size()-].data);
}
注意点:又是一道链表题,可以说是很简单了,一开始忘记写scanf了,一直报内存超限,还以为是一道要用神奇方法解决的题目。要注意的就是输出0 -1的情况,链表为空。
PAT A1052 Linked List Sorting (25 分)——链表,排序的更多相关文章
- PAT 甲级 1028 List Sorting (25 分)(排序,简单题)
1028 List Sorting (25 分) Excel can sort records according to any column. Now you are supposed to i ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- Pat 1052 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 分)
题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...
- PAT A1052 Linked List Sorting
题意:给出N个结点的地址address.数据域data以及指针域next,然后给出链表的首地址,要求把在这个链表上的结点按data值从小到大输出.样例解释:按照输入,这条链表是这样的(结点格式为[ad ...
- 1052 Linked List Sorting (25分)
题目 1. 思路 使用map存放所有的地址对 使用起始地址遍历map,结果存放在vector中 排序vector 输出vector 2. 注意点 开始的时候起始地址为-1 可能有些节点没有用到,注意排 ...
- PAT A1141 PAT Ranking of Institutions (25 分)——排序,结构体初始化
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- PAT A1016 Phone Bills (25 分)——排序,时序
A long-distance telephone company charges its customers by the following rules: Making a long-distan ...
- PAT A1109 Group Photo (25 分)——排序
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)
题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self ...
- 2018-08-27 使用JDT核心库解析JDK源码后初步分析API命名
源自术语词典API项目 · Issue #85 · program-in-chinese/overview, 打算先用早先的代码提取JDK API中的类/方法/参数名, 看看有哪些词需要翻译. 源码在 ...
- RPC框架学习总结
1.RPC是一种技术框架的称呼,不是某种具体协议,不局限于某种协议,RPC顾名思义就是远程过程调用,其核心思想是,RPC客户端调用远程服务器上的接口完成过程调用,远程服务器把结果返回. 2.RPC的最 ...
- Android--记录莫名其妙的引用、依赖冲突解决办法
这几天一直在忙于其他项目,打开之前的项目,我曹,代码什么都没动,为何莫名其妙的的就出现冲突了.(我猜测是依赖的其它三方库,库里面的依赖发生改变导致的) 全是这种错误,各种冲突导致的,记录一下,以下指示 ...
- 新的 Centos 服务器初始化配置
当你初次创建新的 Centos 服务器的时候, Centos 默认的配置安全性和可用性上会存在一点缺陷(运维人员往往会有初始化的脚本).为了增强服务器的安全性和可用性,有些配置你应该尽快地完成. 这篇 ...
- Spark SQL整体架构
0.整体架构 注意:Spark SQL是Spark Core之上的一个模块,所有SQL操作最终都通过Catalyst翻译成类似的Spark程序代码被Spark Core调度执行,其过程也有Job.St ...
- Bresenham算法的实现思路
条件已知两个点的坐标p1(x0,y0),p2(x1,y1)要求画出这条直线 之后的e代表每次的误差积累,初始值为0,可以计算出斜率为k=dy/dx=(y0-y1)/(x0-x1) 1.x为阶跃步长(直 ...
- [20181109]12cR2 的pre_page_sga参数
[20181109]12cR2 的pre_page_sga参数.txt --//12CR2改变了参数pre_page_sga设置为True.设置为true有好处也有缺点.--//先看看官方的定义:ht ...
- 自动化测试基础篇--Selenium等待时间
摘自https://www.cnblogs.com/sanzangTst/p/8376221.html 当你觉得你的定位没有问题,但是却直接报了元素不可见,那你就可以考虑是不是因为程序运行太快或者页面 ...
- Python输出和输入
一.input()函数 在 Python 中,使用内置函数 input()可以接收用户的键盘输入. input()函数的基本用法如 下: variable = input("提示文字") 其中,var ...