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
Address
is the position of the node,Data
is an integer, andNext
is 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 ...
随机推荐
- go web编程——路由与http服务
本文主要讲解go语言web编程中的路由与http服务基本原理. 首先,使用go语言启动一个最简单的http服务: package main import ( "log" " ...
- 并行开发 2.task
原文:8天玩转并行开发——第二天 Task的使用 在我们了解Task之前,如果我们要使用多核的功能可能就会自己来开线程,然而这种线程模型在.net 4.0之后被一种称为基于 “任务的编程模型”所冲击, ...
- datagridview里面的checkbox全选和取消全选
全选 设置全选button,选中所有的checkbox private void selectAll_Click(object sender, EventArgs e) { //遍历datagridv ...
- 安装Sublime Text 3 的过程
在Sublime Text官网下载最新的APP https://www.sublimetext.com 我下载的是 3143 安装之后开始在网上找注册码 我用了成功的注册码的是 这个应该是单个用户的, ...
- redis缓存切面实现(支持缓存key的spel表达式)
1.定义注解 package com.g2.order.server.annotation; import java.lang.annotation.ElementType; import java. ...
- openstack创建一个虚拟机的过程
为什要用云? 一.简单的说就是对资源更加合理的分配,使用,比如硬件的数量,带宽等等这些,因为你不能机器买来不需要了再卖掉(当然也可以),带宽跟机房签合同得来一年的,中间不够了也不能加,超了也不退钱 ...
- Mongodb Capped Collection集合
MongoDB 固定集合(Capped Collections)是性能出色且有着固定大小的集合,对于大小固定,我们可以想象其就像一个环形队列,当集合空间用完后,再插入的元素就会覆盖最初始的头部的元素! ...
- bzoj1002 轮状病毒 暴力打标找规律/基尔霍夫矩阵+高斯消元
基本思路: 1.先观察规律,写写画画未果 2.写程序暴力打表找规律,找出规律 1-15的答案:1 5 16 45 121 320 841 2205 5776 151 ...
- Java打印流学习
打印流 打印流的主要功能是用于输出,在整个IO包打印流分为两种类型,打印流可以很方便的进行输出. 1.字节打印流:PrintStream(在字节输出时,可以增强输出功能) 2.字符打印流:PrintW ...
- centos 6.5 查看发行版本
cat /etc/redhat-release 其他发行版 lsb_release -a