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 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 (≤) 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, 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<cstdio>
#include<iostream>
#include<algorithm>
using namespace std; #define MAXSIZE 1000010 struct node
{
int data;
int next;
} node[MAXSIZE]; int List[MAXSIZE]; int main()
{
int begin,n,k;
cin >> begin >> n >> k;
int Address,Data,Next;
for (int i = ; i < n; i++)
{
cin >> Address >> Data >> Next;
node[Address].data = Data;
node[Address].next = Next;
} int count = ;
int p = begin;
while (p != -)
{
List[count++] = p;
p = node[p].next;
} for(int i = ; i + k <= count; i += k)
{
reverse(&List[i],&List[i+k]);
}
/*
int i = 0;
while (i + k <= count)
{
reverse(&List[i],&List[i+k]);
i += k;
}
*/
for(int i = ; i < count; i++)
{
if(i < count - )
{
printf("%05d %d %05d\n",List[i],node[List[i]].data,List[i+]);
}
else
{
printf("%05d %d -1",List[i],node[List[i]].data);
}
} return ;
}
02-线性结构3 Reversing Linked List (25 分)的更多相关文章
- 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 ...
- pat02-线性结构1. Reversing Linked List (25)
02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...
- 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-线性结构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 ...
- 浙大数据结构课后习题 练习二 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甲级】1074 Reversing Linked List (25 分)
题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...
- PAT甲级1074 Reversing Linked List (25分)
[程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...
- 02-线性结构3 Reversing Linked List
02-线性结构3 Reversing Linked List (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...
- 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 ...
随机推荐
- java之spring mvc之helloworld
这篇主要讲解springmvc的基本的使用,这里以helloworld项目为例. 目录结构: 1. 新建 web 项目 :springmvc_helloworld 2. 在 WebRoot\WEB-I ...
- C# vb .net实现焦距柔化特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的焦距柔化效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- form表单提交数据给后台
1.完整登录示例 1. form表单往后端提交数据注意三点 1.所有获取用户输入标签都应该放在form表单里面 2.action属性控制往哪儿提交,method一般都是设置成post 3.提交按钮必须 ...
- jetbrains全家桶激活
56ZS5PQ1RF-eyJsaWNlbnNlSWQiOiI1NlpTNVBRMVJGIiwibGljZW5zZWVOYW1lIjoi5q2j54mI5o6I5p2DIC4iLCJhc3NpZ25lZ ...
- MapReduce1.x与MapReduce2.x差异
一.MapReduce1.x简介 1.图解 2.JobTracker 主节点,单点,负责调度所有的作用和监控整个集群的资源负载. 3.TaskTracker 从节点,自身节点资源管理和JobTrack ...
- Mybatis返回表自增id
在Mapper中,设置insert中添加useGeneratedKeys = "true" keyProperty = "id" keyColumn=&qu ...
- 解决maven install报错:java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/InclusionScanException
问题:maven install时,报错:java.lang.NoClassDefFoundError: org/codehaus/plexus/compiler/util/scan/Inclusio ...
- 利用yum下载rpm包并批量安装
一.下载rpm包 方法一:downloadonly 1.yum自动下载RPM包及其所有依赖的包至/root/rpm目录: yum install yum-plugin-downloadonly yum ...
- SpringBoot项目打成jar在linux后台运行
--关闭客户端依然进程存在 nohup java -jar spring-boot-hello-1.0.jar 1>/dev/null 2>&1 & --Ctrl+C后不会 ...
- Gerrit和OpenLDAP服务器集成
Gerrit和OpenLDAP服务器集成 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装LDAP服务器 详情请参考:https://www.cnblogs.com/yinz ...