02-线性结构3 Reversing Linked List   (25分)

  • 时间限制:400ms
  • 内存限制:64MB
  • 代码长度限制:16kB
  • 判题程序:系统默认
  • 作者:陈越
  • 单位:浙江大学

https://pta.patest.cn/pta/test/3512/exam/4/question/62614

Given a constant KKK and a singly linked list LLL, you are supposed to reverse the links of every KKK elements on LLL. For example, given LLL being 1→2→3→4→5→6, if K=3K = 3K=3, then you must output 3→2→1→6→5→4; if K=4K = 4K=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 NNN (≤105\le 10^5≤10​5​​) which is the total number of nodes, and a positive KKK (≤N\le N≤N) 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 NNN lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is an integer, and Next 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
 #include<bits/stdc++.h>
using namespace std; struct Node
{
int data;
int adr,next;
}t;
int N,head,K;
map<int,Node> Input;//用map储存输入序列,地址作为键值,方便通过地址(head,next)找到相关点
stack<Node> Sta,T;
vector<Node> Result(N);//储存结果链表
vector<Node>::iterator it;
int main()
{
/*输入*/
scanf("%d %d %d",&head,&N,&K);
for(int i=;i<N;i++)
{
scanf("%d %d %d",&t.adr,&t.data,&t.next);
Input[t.adr]=t;
}
/*处理*/
int num=;
t=Input.find(head)->second;//找到第一个点
while(num++<N)//对n个元素入栈处理
{
int flag=t.next;
Sta.push(t);
if(t.next!=-)
t=Input.find(t.next)->second;
if(num%K==)//满K个元素,逆序出栈放入Result
{
while(!Sta.empty())
{
Result.push_back(Sta.top());
Sta.pop();
}
if(flag==-)//最后一个结点刚好组成最后K个,结束
break;
continue;
}
else if(flag==-)//最后一个结点多余,将栈中的点出,入,出栈后恢复正序放入Result,结束
{
while(!Sta.empty())
{
T.push(Sta.top());
Sta.pop();
}
while(!T.empty())
{
Result.push_back(T.top());
T.pop();
}
break;
}
}
/*输出*/
it=Result.begin();
printf("%05d %d ",it->adr,it->data);
it++;
for(;it!=Result.end();it++)
{
printf("%05d\n%05d %d ",it->adr,it->adr,it->data);
}
printf("-1\n");
Input.clear();
Result.clear();
return ;
}

02-线性结构3 Reversing Linked List的更多相关文章

  1. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  2. 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 ...

  3. 02-线性结构2 Reversing Linked List

    由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...

  4. 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 ...

  5. 数据结构练习 02-线性结构2. Reversing Linked List (25)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  6. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. 02-线性结构3 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  8. 02-线性结构3 Reversing Linked List

    题目 Sample Input: 00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 ...

  9. PAT02-线性结构3 Reversing Linked List

    题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...

随机推荐

  1. String 对象-->判断是否相等

    1.定义和用法 == 值相等 === 绝对相等(值和类型都相等) 举例: var str = '8' var str1 = 8 console.log(str == str1) console.log ...

  2. NumPy学习2:基本运算

    数组相减: a = array([20, 30, 40, 50])print ab = arange(4)print bc = a-bprint c 结果: [20 30 40 50][0 1 2 3 ...

  3. CentOS安装C函数库的man帮助

    安装linux可能没有安装C的man帮助, 像我安装时选择的是最小化安装就没有, 网上的大多是ubunu的安装方式,或者是C++的man帮助, 都不适合,那么CentOS安装C man手册的方法就是: ...

  4. qt creator源码全方面分析(4-1)

    目录 d指针和q指针 简单示例 q指针 QObject和QObjectPrivate qtcreator中的变体1 qtcreator中的变体2 小结 d指针和q指针 我们在类成员名称和使用d指针中, ...

  5. [模板]SPFA判负环

    目录 一.BFS法判负环 二.DFS法判负环 三.SPFA判正环 一.BFS法判负环 Code: #include<bits/stdc++.h> #define re register # ...

  6. PLSQL Developer 中文乱码踩坑记

    环境 操作系统版本: Windows 7 PL/SQL 版本: 12.0.1.1814 原因 由于 Oracle 服务器端和客户端字符集编码不一致引起的. 注意点 写在最前面,减少踩坑!!! 网上教程 ...

  7. 从python爬虫以及数据可视化的角度来为大家呈现“227事件”后,肖战粉丝的数据图

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...

  8. stand up meeting 12-11

    今天因组员时间问题,并没有集中在一起开会,但士杰当面和天赋同学进行了沟通,在lync与国庆进行了沟通. 天赋与重阳再次进行了了沟通,确定了“单词挑战”与“背单词”这两个模块集成的难度,决定先不进行集成 ...

  9. MySQL笔记总结-DDL语言

    DDL语言 数据类型 一.数值型 1.整型 tinyint.smallint.mediumint.int/integer.bigint 1 2 3 4 8 特点: ①都可以设置无符号和有符号,默认有符 ...

  10. 移动(appium)自动化测试-爬虫的另一种手段

    appium自动化测试环境搭建: 1.Python环境(推荐2.7)和jdk. 2.Adb工具的下载:自己单独下载adb.夜神模拟器自带和Android sdk 3.Apk安装介质:真机.Androi ...