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 ...
随机推荐
- 通过 python ssh库连接并发送命令给设备
import paramiko import time hostname = '192.168.248.156' port = 22 user = 'zhou' passwd = ' paramiko ...
- git 入门教程之安装 git
安装 git git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行,根据自身环境选择安装. Linux 系统 linux 系统安装软件大致有两种途径,一种是利用 ...
- 企业建立成功 DevOps 模式所需应对的5个挑战
[编者按]本文作者为 Kevin Goldberg,主要介绍要想成功部署 DevOps 模式,企业所需应对的5大挑战与问题.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 要给 DevOps ...
- 如何让python嵌入html实现类似php的快速开发,十分有价值
1.在一个文件夹名为www.html3.com的web项目来实现,首先到nginx的配置文件nginx.conf做如下配置 python和html混合编写的文件,我以文件后缀为.phtml,通过服务器 ...
- C#-判断语句(五)
判断语句主要有if...else.switch和 条件?语句1:语句2 三种,而if...else中又有if语句,if...else.if...else if...else和if中嵌套if这几种,但是 ...
- 洗礼灵魂,修炼python(84)-- 知识拾遗篇 —— 网络编程之socket
学习本篇文章的前提,你需要了解网络技术基础,请参阅我的另一个分类的博文:网络互联技术(4)——计算机网络常识.原理剖析 网络通信要素 1.IP地址: 用来标识网络上一台独立的终端(PC或者主机) ip ...
- c/c++ 右值引用
c/c++ 右值引用 转自:https://www.cnblogs.com/catch/p/3500678.html 左值(lvalue)和右值(rvalue)是 c/c++ 中一个比较晦涩基础的概念 ...
- [Hive_add_6] Hive 实现 Word Count
0. 说明 Hive 通过 explode()函数 和 split()函数 实现 WordConut 1. Hive 实现 Word Count 方式一 1.1 思路 将每一行文本变为 Array 数 ...
- Javascript_06_表单验证(离开单项,输入框后提示信息)
Javascript_06_ 表单验证(离开单项,输入框后提示信息) 说明:对于必须输入的入力框,光标离开(使用 onblur方法)时进行检查.假如有错,红色的提示信息直接在该画面的这个输入框的后面显 ...
- #007 C语言大作业学生管理系统第四天
第四天还差恢复已删除学生功能 对于我来说,已经开始很复杂了. 小细节太重要了,边写边出错 1 #include<stdio.h> #include<stdlib.h> #inc ...