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> ...
随机推荐
- HttpWebRequest,HttpWebResponse 使用
目的:工作中已经两次使用了,特此记录一下,并写好注释 /// <summary> /// HttpWebRequest的基本配置 /// </summary> public c ...
- vue入门(三)----使用vue-cli搭建一个单页富应用
上面两节我们说了vue的一些概念,其实说的知识一点基础,这部分知识我觉得更希望大家到官网进行学习,因为在这里说的太多我觉得也只是对官网的照搬照抄而已.今天我们来学习一下vue-cli的一些基础知识,并 ...
- 反欺诈(Fraud Detection)中所用到的机器学习模型
反欺诈应用的机器模型算法,多为二分类算法. 1.gbdt梯度提升决策树(Gradient Boosting Decision Tree,GBDT)算法,该算法的性能高,且在各类数据挖掘中应用广泛,表现 ...
- day05.2-Vim编辑器
一. 安装Vim编辑器和打开新建文件 安装Vim编辑器:apt-get install Vim 新建与打开Vim文件:vim 文件名 二. Vim编辑器三种模式的使用与切换 指令 ...
- Total Commander的初次体验
从汉化新世纪下载到最新的TC张学思版后,运行文件只需依照其提示就可以完成该软件的安装.作为新手初次运行体验了以下功能: 一.目录跳转 1. 初次启动TC软件界面截图: 2. 按下Ctrl+d后,直接再 ...
- 【bzoj2751】[HAOI2012]容易题(easy) 数论-快速幂
[bzoj2751][HAOI2012]容易题(easy) 先考虑k=0的情况 那么第一个元素可能为[1,n] 如果序列长度为m-1时的答案是ans[m-1] 那么合并得 然后同理答案就是 k很小 而 ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- 内核启动后,lcd显示logo失败
针对-s5pv210,但对其他平台也使用 lcd显示logo失败,若显示成功默认的logo是一只企鹅,但是串口打印“Start display and show logo”,但是LCD屏没有显示 ...
- ul li做横向导航栏例子
/* ul li以横排显示 */ /* 所有class为menu的div中的ul样式 */ div.menu ul { list-style:none; /* 去掉ul前面的符号 */ margin: ...
- Codeforces - 151C 质因子分解
显然只需要能跑到第二个因子就赢了 需要特判非平凡因子 常数优化:不用求出所有因子,跑完第二个素数就行了 #include<bits/stdc++.h> using namespace st ...