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 ...
随机推荐
- 纯JS操作服务器绑定控件(Repeat)实现表头升降排序
JS实现功能 var obj = function (id) { return "string" == typeof id ? document.getElementById(id ...
- 大四找实习(web前端),加油
大四很奇妙,课程变少了,事情却繁杂了. 大三暑假去学驾照,在很多人看来太迟了(毕竟身边很多人跑去实习了),包括我自己.学驾照特别费时间,尤其是对即将大四,希望用实习充实自己的我来说.考虑再三,终于决定 ...
- MYSQL基础笔记(五)- 练习作业:站点统计练习
作业:站点统计 1.将用户的访问信息记录到文件中,独占一行,记录IP地址 <?php //站点统计 header('Content-type:text/html;charset=utf-8'); ...
- [Visual Studio] 开启Visual Studio 2012通过右键菜单创建单元测试(Unit Test)
Visual Studio 2012可以说是迄今为止微软VS开发工具中用户体验最好的产品,无论是速度还是体验以及功能,都非常出色,但是,使用了一段时间后发现有一个之前版本VS都有的功能却在Visual ...
- iOS xcode6添加预编译文件
在xcode6以后,由于苹果不建议开发者乱用预编译文件,所以,在项目创建之后 就不会自动生成预编译文件. 那么如果我们想要使用预编译文件,就需要自己动手来添加.那到底该如何为我们的项目添加预编译文件呢 ...
- php.ini 配置文件的深入解析
[PHP] ; PHP还是一个不断发展的工具,其功能还在不断地删减 ; 而php.ini的设置更改可以反映出相当的变化, ; 在使用新的PHP版本前,研究一下php.ini会有好处的 ;;;;;;;; ...
- java 调用OpenOffice将word格式文件转换为pdf格式
一:环境搭建 OpenOffice 下载地址http://www.openoffice.org/ JodConverter 下载地址http://sourceforge.net/projects/jo ...
- DisableExplicitGC和Direct ByteBuffer
直接堆外内存请参见:http://blog.csdn.net/lantian0802/article/details/39257087 JVM调优请参见:http://hllvm.group.itey ...
- (转)C#模拟键盘鼠标事件
原文 1.模拟键盘事件System.Windows.Forms.SendKeys以下是 SendKeys 的一些特殊键代码表. 键 代码 BACKSPACE {BA ...
- 在 Tomcat 中设置 JDBCRealm
除了默认配置的 DataSourceRealm,Tomcat 还支持 JDBCRealm,它通过 JDBC 来访问记录在关系数据库里的认证信息. JDBCRealm 的配置步骤如下: 在 $TOMCA ...