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. ltrace killed by SIGTRAP

    [Ltrace-devel] ltrace stucks with pthreads Heiko Carstens heiko.carstens at de.ibm.comFri Apr 14 11: ...

  2. InnoDB的redo日志管理---饶珑辉

    http://raolonghui.com/2015/06/24/innodb%E7%9A%84redo%E6%97%A5%E5%BF%97%E7%AE%A1%E7%90%86/#comment-11

  3. iOS NSDatePicker

    1.NSDate类 1>NSDate是系统一个日期,时间类 2>就是返回当前的日期,时间 3>+(id)date; 4>返回未来secs秒后的日期,时间 5>+(id)d ...

  4. bootstrap--组件之按钮式下拉菜单

    把任意一个按钮放入 .btn-group 中,然后加入适当的菜单标签,就可以让按钮作为菜单的触发器了. 简单的实现如下 Code<div class="btn-group"& ...

  5. Devexpress 使用经验 —— ASPxGridView命令行自定义按钮灵活使用

    ASPX <dx:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="Ob ...

  6. sql server varchar和nvarchar的区别

    一.前言 在了解varchar 和nvarchar之前咱们先了解一下这些词的字面和常用意思,以方便我们更好的使用: SQL SERVER中生成的语句中,字符串前加N.N 前缀必须是大写字母.是Unic ...

  7. Xcode7 Xcode6 中添加pch文件

    在Xcode7 和 Xcode6 中添加.pch文件是一样的,具体操作图文如下: 第一步:在Xcode的项目里,一般在Supporting Files 文件夹下创建,选中Supporting File ...

  8. 最近在学习UDP方面的通信,找到一个很棒的博客

    http://blog.csdn.net/kesalin/article/details/8798039

  9. 深入理解Javascript之this关键字

    深入理解Javascript之this关键字 作者: Laruence(   ) 本文地址: http://www.laruence.com/2009/09/08/1076.html 转载请注明出处 ...

  10. Prism for WPF 第一讲 Event机制

    在本篇文章中主要讲解在Prism中模块与模块之间事件关联的机制.在这里牵涉到三个名词:事件定义,事件发布,事件订阅. 第一:事件定义 在公共类库中定义事件. ①没有参数事件 public class ...