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 ...
随机推荐
- HTML+CSS实例——漂亮的背景(一)
一.网址:http://www.csszengarden.com/?cssfile=213/213.css 二.效果 三.CSS body { background-color:#F0ECD6; ba ...
- Oracle常用命令13(数据库的启动、关闭)
数据库的启动.关闭 数据库的启动:安装启动.非安装启动.共享启动.独占启动.约束启动.强制启动 --不登陆的方式进入 Sqlplus /nolog 安装启动: Startup {pfile=<f ...
- 关于修改Eclipse工作空间对应的文件夹名称之后的处理.
把文件夹名字从"xhkong"变成"xhkong(maintenance5.6)"之后打开这个工作空间. 导入git仓库我发现了一个之前没有发现的小技巧. 导入 ...
- DAG的动态规划 (UVA 1025 A Spy in the Metro)
第一遍,刘汝佳提示+题解:回头再看!!! POINT: dp[time][sta]; 在time时刻在车站sta还需要最少等待多长时间: 终点的状态很确定必然是的 dp[T][N] = 0 ---即在 ...
- poj 2724 二分图最大匹配
题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...
- css3 颜色记
css3 颜色 opacity 使元素呈现出半透明效果. opacity:alphavalue || inherit alphavalue:默认值为1,可以取0~1任意浮点数.取值为1时,元素完全不透 ...
- Emmet 语法大全(缩写语法/sublime 插件)
Emmet 使用类似于 CSS 选择器的语法描述元素在生成的文档树中的位置及其属性. 元素 可以使用元素名(如 div 或者 p)来生成 HTML 标签.Emmet 没有预定义的有效元素名的集合,可以 ...
- Servlet & JSP - Servlet API Overview
Servlet & Generic & HttpServlet 类图 Servlet 的生命周期 init.service 和 destroy 是 servlet 的生命周期方法,它们 ...
- Unity3D除了在编辑器里,怎么用代码给一个Texture类型的变量赋值
resource.load上来一张贴图就行. using UnityEngine; using System.Collections; public class example : MonoBehav ...
- Android实现双进程守护 (转)
做过android开发的人应该都知道应用会在系统资源匮乏的情况下被系统杀死!当后台的应用被系统回收之后,如何重新恢复它呢?网上对此问题有很多的讨论.这里先总结一下网上流传的各种解决方案,看看这些办法是 ...