PAT1074 Reversing Linked List (25)详细题解
02-1. Reversing Linked List (25)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements 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 (<= 105) which is the total number of nodes, and a positive K (<=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 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, 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
这题用二维数组高维代表首地址啊address,低维[address][0]储存data,[address][1]则储存next,牺牲空间换时间。
再使用维数组list进行reverse.
int list[];
int node[][]; int st,num,r;
cin>>st>>num>>r;
int address,data,next,i;
for(i=;i<num;i++)
{
cin>>address>>data>>next;
node[address][]=data;
node[address][]=next;
}
先输入node值。如图
| address | 00000 | 00100 | 12309 | 33218 | 68237 | 99999 |
| data | 4 | 1 | 2 | 3 | 6 | 5 |
| next | 99999 | 12309 | 33218 | 00000 | -1 | 68237 |
值得说明的是:不需要按照顺序输入,到数组中后自然会对应数组的下标值即address。
因此上表中按照数组顺序排序,无初始值的数组下标省略不列。
下面对list赋address.
int m=,n=st;
while(n!=-)
{
list[m++]=n;
n=node[n][];
}
| list | 0 | 1 | 2 | 3 | 4 | 5 |
| address | 00100 | 12309 | 33218 | 00000 | 99999 | 68237 |
其中list[0]储存的是st的address,list[1]储存的是st->next的地址,以此类推。直到遇到address为-1.
i=;
while(i+r<=m)
{
reverse(list+i,list+i+r);
i=i+r;
}
进行反转,使用algorithm的reverse函数。
for (i = ; i < m-; i++)
{
printf("%05d %d %05d\n", list[i], node[list[i]][], list[i+]);
}
printf("%05d %d -1\n", list[i], node[list[i]][]);
最后进行输出,注意的是以int形式储存的,如address中的00000实际储存位置是0,故输出时注意是要用五位数输出。
完整AC代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
int list[];
int node[][];
int main()
{
freopen("F:\\in1.txt","r",stdin);
int st,num,r;
cin>>st>>num>>r;
int address,data,next,i;
for(i=;i<num;i++)
{
cin>>address>>data>>next;
node[address][]=data;
node[address][]=next;
}
int m=,n=st;
while(n!=-)
{
list[m++]=n;
n=node[n][];
}
i=;
while(i+r<=m)
{
reverse(list+i,list+i+r);
i=i+r;
}
for (i = ; i < m-; i++)
{
printf("%05d %d %05d\n", list[i], node[list[i]][], list[i+]);
}
printf("%05d %d -1\n", list[i], node[list[i]][]);
}
PAT1074 Reversing Linked List (25)详细题解的更多相关文章
- 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 ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 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)-求反向链表
题意说的很清楚了,这种题的话,做的时候最好就是在纸上自己亲手模拟一下,清楚一下各个指针的情况, 这样写的时候就很清楚各个指针变量保存的是什么值. PS:一次AC哈哈,所以说自己动手在纸上画画还是很有好 ...
- 数据结构练习 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 ...
- PAT 1074. 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 ...
- 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-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 ...
- PAT Advanced 1074 Reversing Linked List (25) [链表]
题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...
随机推荐
- java中的选择排序之降序排列
import java.util.Arrays;//必须加载 class Demo{ public static void main(String []args){ int[] arr={3,54,4 ...
- crontab中引入环境变量(比如需要执行tomcat的关闭启动)
起因 crontab中的定时任务,执行到关闭tomcat时,报环境变量找不到 解决方案 1.使用 . /etc/profile 引入环境变量 ###推荐, 实测ubuntu12 成功 2.使用 sou ...
- Canvas画布实现自定义时钟效果
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- html路径问题
1.绝对路径 绝对路径是指文件在硬盘上真正存在的路径.例如"bg.jpg"这个图片是存放在硬盘的"E:\book\网页布局代码\第2章"目录下,那么 ...
- laravel中间件使用
1.在app/Http/Kernel.php文件中配置中间件文件,例如: protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Mi ...
- PVM的安装和编译PVM程序
最近刚开始学习并发编程,学习到了PVM这一块.关于在linux系统中PVM的安装,真是要我的命,繁琐死了,最关键是我对linux也是刚开始学,还在继续学习<鸟哥的linux私房菜>一书.好 ...
- C++ 宏定义#define 中##的使用
在C++的宏定义中,符号##一般是用于连接,包括参数的连接,参数与标识符的连接等,然后形成一个新的标识符. 下面举几个例子来进行说明. eg1: #define ADD(a,b) a##b #defi ...
- Bootstrap列表组
前面的话 列表组是Bootstrap框架新增的一个组件,可以用来制作列表清单.垂直导航等效果,也可以配合其他的组件制作出更漂亮的组件.本文将详细介绍Bootstrap列表组 基础列表组 基础列表组,看 ...
- Google调试工具
f11 逐语句,到过程里,f10逐过程,跳到下一个off,f8调到下一个断点执行.
- FileInputStreamTest
package JBJADV003;import java.io.FileNotFoundException;import java.io.IOException;import java.io.Inp ...