pat1052. Linked List Sorting (25)
1052. 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<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<stack>
using namespace std;
struct node{
int k,next;
};
#define h 100000
node line[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int first,n;
scanf("%d %d",&n,&first);
if(first==-){//链表可能为空,这个太坑!!
printf("0 -1\n");
return ;
}
int i,add,k,next;
for(i=;i<n;i++){
scanf("%d %d %d",&add,&k,&next);
line[add].k=k;
line[add].next=next;
}
line[h].next=-;
int p,pp,cur,count=;
next=line[first].next;
line[first].next=line[h].next;
line[h].next=first;
while(next!=-){//insert
cur=next;
pp=h;//头节点,指向p的前一个节点
p=line[h].next; //cout<<"p: "<<p<<" "<<line[p].k<<endl;
//cout<<"cur: "<<cur<<" "<<line[cur].k<<endl; while(p!=-&&line[p].k<line[cur].k){
pp=p;
p=line[p].next; //cout<<pp<<" "<<p<<endl; } //cout<<"cur"<<endl; next=line[cur].next;
line[cur].next=p;
line[pp].next=cur;
count++;
}
next=line[h].next;
printf("%d %05d\n",count,next);
while(next!=-){//遍历
printf("%05d %d ",next,line[next].k);
if(line[next].next==-){
printf("%d",line[next].next);
}
else{
printf("%05d",line[next].next);
}
printf("\n");
next=line[next].next;
}
return ;
}
pat1052. Linked List Sorting (25)的更多相关文章
- 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 (25)(25 分)
1052 Linked List Sorting (25)(25 分) A linked list consists of a series of structures, which are not ...
- 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 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- 1052. Linked List Sorting (25)
题目如下: A linked list consists of a series of structures, which are not necessarily adjacent in memory ...
- PAT甲题题解-1052. Linked List Sorting (25)-排序
三个注意点: 1.给出的n个节点并不一定都在链表中 2.最后一组样例首地址即为-1 3.输出地址的时候一直忘记前面要补0... #include <iostream> #include & ...
- PAT Advanced 1052 Linked List Sorting (25) [链表]
题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. W ...
- PAT (Advanced Level) 1052. Linked List Sorting (25)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
随机推荐
- python web框架(bottle,flask,tornado)
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. pip i ...
- C#用GDI+解析Json文件绘制Chart
using System.Collections.Generic; namespace Chart { public class Program { static void Main(string[] ...
- CentOS 6.7中安装python3.5
1.安装一些依赖的软件包 yum groupinstall "Development tools" yum install zlib-devel bzip2-devel opens ...
- 「BZOJ 3270」博物馆「高斯消元」
应该算高斯消元经典题了吧. 题意:一个无向连通图,有两个人分别在\(s,t\),若一个人在\(u\),每一分钟有\(p[u]\)的概率不动,否则随机前往一个相邻的结点,求在每个点相遇的概率 题解: 首 ...
- Go语言资源教程:Redis介绍安装和使用
Redis的操作和使用 在安装好redis以后,我们这里给大家讲一下redis操作的一些知识. Redis支持的数据类型:string,hash,list,set,sorted set 我们来练习一下 ...
- 解决“System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本”的问题
以server2008为例: 首先确保使用sqlplus能访问数据库. 1.管理工具->计算机管理->本地用户和组->组->administrators属性,添加,高级,立即查 ...
- ORACLE 中dbms_stats的使用
dbms_stats能良好地估计统计数据(尤其是针对较大的分区表),并能获得更好的统计结果,最终制定出速度更快的SQL执行计划. exec dbms_stats.gather_schema_stats ...
- 洛谷2685 [TJOI2012]桥
[TJOI2012]桥 题目大意:给定一无向图,求删除一条边后1到n最短路的最大值,以及方案数. 做法:我们先从1为起点.从n为起点跑两边dij,获得每一个点到起点1.终点n的最短距离,其实距离和边权 ...
- git 创建、切换和提交新分支
查看本地分支 git branch 创建新的分支 git branch <newBranch> 切换分支 git checkout <branchName> 创建并切换分支 g ...
- P3167 [CQOI2014]通配符匹配 题解
题目 题目大意 给出一个字符串,其中包含两种通配符 ‘?’和 ‘*’ ,‘?’可以代替一个字符,‘*’可以代替一个字符串(长度可以为0) 然后给出几个字符转,判断能否用给出的字符串表示出来 样例解释 ...