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 提交了10次才完全正确,花了快5个小时。感觉自己实力好差。
#include<iostream>
#include <string>
using namespace std;
class linked{
public:
int data;
int next;
};
linked *list = new linked[];
int prehead;
int nexthead;
int Reverse(int head, int k){
int cnt = ,temp;
int news = head;
int olds = list[head].next;
while (cnt < k){
temp = list[olds].next;
list[olds].next = news;
news = olds;
olds = temp;
cnt++;
}
list[head].next = olds;
prehead = head;
nexthead = olds;
return news;
}
int main(){
int firstNode, address, next,N, k, data,head,preshead;
int sum = ;
cin >> firstNode >> N >> k;
for (int i = ; i < N; i++){
cin >> address >> data >> next;
list[address].data = data;
list[address].next = next;
}
int p = firstNode;
while (p != -){
sum++;
p = list[p].next;
}
head = Reverse(firstNode, k);
for (int i = ; i < sum / k; i++){
preshead = prehead;
list[preshead].next = Reverse(nexthead, k);
}
p = head;
while (p!= -){
printf("%05d ",p);
cout << list[p].data <<" ";
if (list[p].next != -)
printf("%05d\n", list[p].next);
else
cout << list[p].next << endl;
p = list[p].next;
} return ;
}

ac的图片看着真的很爽。

数据结构练习 02-线性结构2. Reversing Linked List (25)的更多相关文章

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

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

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

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

  4. 浙大数据结构课后习题 练习二 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 ...

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

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

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

  7. Java数据结构介绍(线性结构和非线性结构)

    数据结构包括:线性结构和非线性结构. 线性结构 数据元素之间存在一对一的线性关系 包括顺序存储结构和链式存储结构.顺序存储的线性表称为顺序表,顺序表中的存储元素是连续的 链式存储的线性表称为链表,链表 ...

  8. PAT1074 Reversing Linked List (25)详细题解

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

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

随机推荐

  1. 【转】最新基于adt-bundle-windows-x86的android开发环境搭建

    http://blog.csdn.net/wangqiuyun/article/details/8731240 某系统要配套做一个android客户端,来一次android开发环境快速搭建,系统Win ...

  2. 集合(Collection,set,list,map)

    package cn.hncu.col.col; import java.util.ArrayList;import java.util.Collection;import java.util.Has ...

  3. NIO学习:使用Channel、Buffer写入文件

    NIO的效率要高于标准IO,因为NIO将最耗时的IO操作(填充和提取缓冲区)转移会操作系统.NIO以块为单位传输数据,相比标准IO的以字节为单位效率要高很多. 通道和缓冲时NIO的核心对象,每个NIO ...

  4. quartz多个scheduler实现

    1.首先排除错误用法 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = sche ...

  5. Lambda表达式(C语言-gcc编译器)

    前言 先来啰嗦几句,身边的朋友,同事都喜欢在博客上记录自己在学习计算机编程技术路程上的一些问题和心得.就我个人而言,这是个好习惯,特别是在计算机编程领域,技术方向种类繁多,永无止境.所以,我也开始我人 ...

  6. 45种Javascript技巧大全【转藏】

    JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...

  7. 20160421javaweb之上传下载小案例---网盘

    一.建立数据库: CREATE TABLE IF NOT EXISTS `netdisk` ( `id` ) NOT NULL AUTO_INCREMENT, `uuidname` ) NOT NUL ...

  8. Haproxy配置参数

    HAProxy配置中分成五部分内容,当然这些组件不是必选的,可以根据需要选择部分作为配置. ===================== global    参数是进程级的,通常和操作系统(OS)相关. ...

  9. 关于网页强制被跳转到wpkg.org的解决

    今天登陆MIT的网站看一篇文章,在进入到页面的时候,网页就会自动跳转到wpkg.org这个网页,查了下据说是DNS被污染了,暂时还是不是很清楚,先把问题解决了. 方法: 在C:\WINDOWS\sys ...

  10. Ubuntu系统的安装

    在上一篇博客中,我们已经建立了一个“空白”的虚拟Ubuntu镜像,在这篇博客中,我们将介绍如何安装并进入完整的Ubuntu系统. 写在前面:不同版本的系统在安装过程中,有些操作可能会不同,但是其核心步 ...