Linked List Sorting (链表)
Linked List Sorting (链表)
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
坑点:1、可能是空链表 2、并不是每个节点都在链表上的
#include <iostream>
#include <vector>
#include<iomanip>
#include <algorithm>
using namespace std;
struct node
{
int add;
int data;
int next;
};
node a[100000],b[100000];
bool cmp(node n1,node n2)
{
return n1.data<n2.data;
}
int main()
{
int len,fir,add,data,next;
int i;
while(cin>>len)
{
cin>>fir;
for(i=0;i<len;i++)
{
cin>>add>>data>>next;
node n;
n.add=add;
n.data=data;
n.next=next;
a[add]=n;
}
if(fir==-1)
{
cout<<"0 -1"<<endl;
continue;
}
i=0;
while(fir!=-1)
{
b[i].add =a[fir].add;
b[i].data =a[fir].data;
b[i].next =a[fir].next;
fir = a[fir].next;
++i;
}
int len=i;
sort(b,b+len,cmp);
for(i=0;i<len-1;i++)
{
b[i].next=b[i+1].add;
}
b[len-1].next=-1;
cout<<len<<" "<<setfill('0')<<setw(5)<<b[0].add<<endl;
for(i=0;i<len;i++)
{
if(b[i].next==-1)
cout<<setfill('0')<<setw(5)<<b[i].add<<" "<<b[i].data<<" "<<-1<<endl;
else
cout<<setfill('0')<<setw(5)<<b[i].add<<" "<<b[i].data<<" "<<setfill('0')<<setw(5)<<b[i].next<<endl;
}
}
return 0;
}
Linked List Sorting (链表)的更多相关文章
- 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) A linked list consists of a series of structures, which are not neces ...
- PAT1052:Linked List Sorting
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- PAT 1052 Linked List Sorting [一般]
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not nece ...
- Pat 1052 Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- pat1052. Linked List Sorting (25)
1052. Linked List Sorting (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A ...
- Linked List Sorting
静态链表(用结构体数组模拟链表) 1052 Linked List Sorting (25分) A linked list consists of a series of structur ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- 【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 ...
随机推荐
- TextFiled 中输入金额
要求: 输入的金额不能超过六位, 小数点后面只能输入两位小数 如果 textFIled 中第一位输入的是0 ,后面必须输入小数点,否则禁止输入 用到 textfiled代理方法 #pragma ma ...
- hibernate 插件安装
安装hibernate插件 1,help--install new software work with选择All Available Sites 搜索框输入hibernate 会出现所有hiber ...
- Mac Yosemite OS10.10 Apache 虚拟主机设置
昨天睡觉前开始挂机下载OS10.10 Yosemite,早上6点半就很兴奋地起来安装新系统了.安装完成后打开界面,感觉真的大不一样了,很优很美,很喜欢. 在下载各种更新,体验各种新功能的同时,我也不忘 ...
- [ImportNew]Java中的Timer类和TimerTask类
http://www.importnew.com/9978.html java.util.Timer是一个实用工具类,该类用来调度一个线程,使它可以在将来某一时刻执行. Java的Timer类可以调度 ...
- (转)9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
原文:http://www.cnblogs.com/figure9/archive/2013/01/09/2853649.html 1,简介 毕业答辩搞定,总算可以闲一段时间,把这段求职经历写出来,也 ...
- 微信 redirect_uri参数错误 正确的处理
如果您若成功将微信搭建了到自己的服务器中的情况下,进行网页授权时出现如下图 解决方案: 开发->接口权限->找到类目为"网页服务->网页账号" 点击修改,注意,此 ...
- java演示适配器(adapter)模式
为什么要使用模式: 模式是一种做事的一种方法,也即实现某个目标的途径,或者技术. adapter模式的宗旨就是,保留现有类所提供的服务,向客户提供接口,以满足客户的需求. 类适配器:客户端定义了接口并 ...
- JQuery中操作表单和表格
一:表单应用 1.HTML中的表单大致由三部分组成 (1).表单便签:包含处理表单数据所用的服务端程序URL,以及数据提交到服务器的方法. (2).表单域:包含文本框.密码框.隐藏域.多行文本框.复选 ...
- stl中的map数据类型
1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...
- PDA库位商品出库适配算法
功能描述 代码实现 算法需求 回到顶部 功能描述 在PDA上面,通过扫描订单号,查询该商品的款号,尺码,订单数:同时适配仓库库位商品[SKU],显示该商品所在库位,库存数量,适配数. 因为PDA界 ...