PAT B1025/A1074

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

给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。

输入格式:

每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<= 105)、以及正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。

接下来有N行,每行格式为:

Address Data Next

其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。

输出格式:

对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。

输入样例:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
输出样例:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "stdio.h"
//#include "math.h"
#include "string.h"
//#include "iostream"
#include "stdlib.h"
//#include "vector"
//#include "set"
//#include "map"
//#include "stack"
#include "queue"
#include "algorithm"
using namespace std;

//typedef long long LL;
const int maxn = 100010;
struct Node{
int address, data, next;
int order;//结点在链表上的序号,无效结点记为maxn
}node[maxn];
bool cmp(Node a, Node b){
if (a.order == -1 || b.order == -1) {
return a.order > b.order;
}else{
return a.order < b.order;
}//按order从小到大排序
}
int main(){
for (int i = 0; i < maxn; i++) {
node[i].order = -1;//初始化全部为无效结点
}
int begin, n, K, address;
scanf("%d%d%d", &begin, &n, &K);//起始地址、结点个数、步长
for (int i = 0; i < n ; i++) {
scanf("%d", &address);
scanf("%d%d", &node[address].data, &node[address].next);
node[address].address = address;
}
int p = begin, count = 0;//count计数有效结点的数目
while (p != -1) {
node[p].order = count++;//结点在单链表中的序号
p = node[p].next;//下一个结点
}
sort(node, node + maxn, cmp);//按单链表从头到尾顺序排列
//有效结点为前count个结点,为了下面书写方便,因此把count赋值给n
n = count;
//单链表已经形成,下面是按题目要求的输出
for (int i = 0; i < n / K; i++) {//枚举完整的n/K块
for (int j = (i + 1) * K - 1; j > i * K; j--) {//第i块倒着输出
printf("%05d %d %05d\n", node[j].address,node[j].data,node[j-1].address);
}
//下面是每一块最后一个结点的next地址的处理
printf("%05d %d ", node[i*K].address, node[i*K].data);
if (i < n/K -1) {//如果不是最后一块,就指向下一块的最后一个结点
printf("%05d\n", node[(i + 2)*K -1].address);
}else{//是最后一块时
if (n%K == 0) {//恰好是最后一个结点,输出-1
printf("-1\n");
}else{//剩下不完整的块按原先的顺序输出
printf("%05d\n", node[(i+1)*K].address);
for (int i = n/K * K; i < n; i++) {
printf("%05d %d ", node[i].address, node[i].data);
if (i < n - 1) {
printf("%05d\n", node[i + 1].address);
}else{
printf("-1\n");
}
}
}
}
}

return 0;
}