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 ...
随机推荐
- 用GraphicsMagick处理svg转png遇到的坑
1前言 用GraphicsMagick处理svg转png,且背景是透明且没有黑边,由于使用虚拟机的gm版本是1.3.28导致有黑边问题且svg中path中有opacity属性时,加上+antialia ...
- Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口
RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...
- js正则表达式(七)
一.正则表达式对象的创建方法一:使用构造函数的形式: var pattern = new RegExp('正则表达式','修饰符'); var pattern = new RegExp('hello' ...
- ABAP和Java里的单例模式攻击
面向对象编程世界里的单例模式(Singleton)可能是设计模式里最简单的一种,大多数开发人员都觉得可以很容易掌握它的用法.单例模式保证一个类仅有一个实例,并提供一个访问它的全局访问点. 然而在某些场 ...
- postman 在pre-request中发送application/x-www-form-urlencoded 格式表单
postman中在pre-request 发送请求 知识点: json数据解析和遍历 application/x-www-form-urlencoded表单 Array基本使用 js函数 http请求 ...
- MySQL Replication--复制基本工作原理
复制工作原理(1) master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events):(2) slave将master的binary lo ...
- python(写入excel操作-xlwt模块)
一.安装xlwt模块 pip install xlwt 二.excel写入操作 这种方式只能新增或者覆盖文件写入 import xlwt # 创建一个workbook 设置编码 workbook = ...
- Docker 安装HDFS
网上拉取Docker模板,使用singlarities/hadoop镜像 [root@localhost /]# docker pull singularities/hadoop 查看: [root@ ...
- Activity知识点详解
Activity知识点详解 一.什么是Activity 官方解释: The Activity class is a crucial component of an Android app, and t ...
- CentOS7:sorry,that didn't work.please try again!
参考以下解决方案,重点是vi etc/selinux/config 把 enforcing 改为 disable 应用场景 linux管理员忘记root密码,需要进行找回操作.注意事项:本文基于cen ...