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. Winedt10 添加自定义宏

    Winedt10 添加自定义功能,并在toolbar上添加快捷命令 功能描述: 用宏实现latex+bib参考文献的一键编译. Remark: The toolbar is the most visi ...

  2. SQLServe错误整理

    1. sql拒绝访问?提示SQL Serve不存在或访问被拒绝 (SQL Server does not exist or access denied.) (A) 1:你如果是独立上网的请把21端口打 ...

  3. 一个java文件编译之后会产生多个class文件

    如图所示:如果编译后一个java文件中类有内部类的话,就会编译产生多个类

  4. Unity3D 之2D动画机

    这里来讲解一下2D动画机的使用 2D的时候,默认的情况下,可以调用默认的站立之类的动画,然后通过触发,可以变化自己的动画. 一:将一个图切成一些一个元素 二:创建一个精灵,给精灵添加一个动画机 三:给 ...

  5. Java反序列化

    一.序列化和反序列化的概念       把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬 ...

  6. ionic(一) build你的第一个android apk

    1.ionic start myApp tabs    >>创建一个app 2.cd myApp  >>进入myApp文件 3.ionic platform add andro ...

  7. C#数组按值和按引用传递数组区别

    C#中,存储数组之类对象的变量并不是实际存储对象本身,而是存储对象的引用.按值传递数组时,程序将变量传递给方法时,被调用方法接受变量的一个副本,因此在被调用时试图修改数据变量的值时,并不会影响变量的原 ...

  8. html调用applet

    1.相同目录下 <applet code="*.class" width=250 height=50> </applet> 指定applet类名称,appl ...

  9. struts2 I18n问题 国际化

    java国际化 1.了解缺省Locale是由操作系统决定的,Locale是由语言和国家代码组成 2.国际化资源文件是由baseName+locale组成,如:MessageBundle_en_US.p ...

  10. 窗口 namedWindow(), destroyWindow(), destroyAllWindows()[OpenCV 笔记6]

    void namedWindow(const string& winname, int flags=WINDOW_AUTOSIZE); 创建一个窗口.imshow直接指定窗口名,可以省去此函数 ...