PAT_A1074#Reversing Linked List
Source:
Description:
Given a constant K and a singly linked list L, you are supposed to reverse the links of every Kelements on L. For example, given L being 1→2→3→4→5→6, if K=3, then you must output 3→2→1→6→5→4; if K=4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤) which is the total number of nodes, and a positive K (≤) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where
Addressis the position of the node,Datais an integer, andNextis the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1
Keys:
- 栈和队列
Attention:
- vector<int> ans.insert(ans.begin()+pos,x);
Code:
#include<cstdio>
#include<stack>
#include<vector>
using namespace std;
const int M=1e5+;
struct node
{
int data;
int addr,next;
}link[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int first,n,k,address;
scanf("%d%d%d", &first,&n,&k);
for(int i=; i<n; i++)
{
scanf("%d", &address);
link[address].addr=address;
scanf("%d%d", &link[address].data,&link[address].next);
}
int pos=;
stack<int> re;
vector<int> ans;
while(first != -)
{
re.push(first);
if(++pos == k)
{
pos=;
while(!re.empty())
{
ans.push_back(re.top());
re.pop();
}
}
first = link[first].next;
}
int len=ans.size();
while(!re.empty())
{
ans.insert(ans.begin()+len,re.top());
re.pop();
}
for(int i=; i<ans.size(); i++)
{
if(i!=)
printf("%05d\n", ans[i]);
printf("%05d %d ", ans[i],link[ans[i]].data);
}
printf("-1"); return ;
}
PAT_A1074#Reversing Linked List的更多相关文章
- PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 1074 Reversing Linked List[链表][一般]
1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】
02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...
- PTA 02-线性结构3 Reversing Linked List (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List (25分) Given a ...
- PAT 甲级 1074 Reversing Linked List (25 分)(链表部分逆置,结合使用双端队列和栈,其实使用vector更简单呐)
1074 Reversing Linked List (25 分) Given a constant K and a singly linked list L, you are supposed ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
- 想了很久,一道Microsoft的笔试题目 —— Reversing Linked List
Reversing Linked List Given a constant K and a singly linked list L, you are supposed to reverse the ...
- 02-线性结构2 Reversing Linked List
由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...
随机推荐
- css 绘制checkbox,radio
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 移动app云测试平台
一:移动App云测试平台 1.云测试平台背景 随着智能手机的普及率和渗透率越来越高,App开发软件也越来越多.但是因为安卓和IOS的碎片化,尤其是安卓,因为完全开源的原因,导致设备繁多,品牌众多,版本 ...
- Python入门习题3.天天向上
例3.1 一年365天,以第一天的能力值为基数,记为1.0,当好好学习时能力值相比前一天提高1%,当没有学习时能力值相比前一天下降1%.每天努力(dayup)和每天放任(daydown),一年下来的能 ...
- BZOJ 4821 (luogu 3707)(全网最简洁的代码实现之一)
题面 传送门 分析 计算的部分其他博客已经写的很清楚了,本博客主要提供一个简洁的实现方法 尤其是pushdown函数写得很简洁 代码 #include<iostream> #include ...
- 最长公共上升子序列 (LIS+LCS+记录)
[题目描述] 给出两个序列,求出最长公共上升子序列的长度,并输出其中一个解. [题目链接] http://noi.openjudge.cn/ch0206/2000/ [算法] 经典问题,结合了LIS和 ...
- Quartz的简单使用
一.Quartz 介绍 Quartz是Java领域最著名的.功能丰富的.开放源码的作业调度工具,几乎可以在所有的Java应用程序中集成--从小的单机应用到大的电子商务系统. Quartz可以用来执行成 ...
- JS高级 — 函数的声明和表达式
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 有关css的兼容问题
兼容性 1 页面在不同浏览器中可能显示不同 在IE6下 子级的宽度会撑开父级设置好的宽度 温馨提示:和模型的计算一定要精确,IE浏览器可能显示不同 兼容性 2 在IE6中,元素浮 ...
- struts2中的Action实现的三种方式
Action类创建方式有哪些? 方式一:直接创建一个类,可以是POJO,即原生Java类,没有继承任何类,也没有实现任何接口 这种方式使得strust2框架的代码侵入性更低,但是这种方式是理想状态,开 ...
- SQL必知必会学习笔记
2.5 select SELECT 要返回的列或表达式 是FROM 从中检索数据的表 仅在从表选择数据时使用WHERE 行级过滤 ...