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 (≤10​5​​) 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

 #include <stdio.h>
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
#include <math.h>
#include <vector>
using namespace std;
const int maxn = ;
struct node{
int addr;
int data;
int next;
}nodes[maxn];
vector<node> v;
int main(){
int st, n, k, count = ;
cin >> st >> n >> k;
for (int i = ; i < n; i++){
int start, data, next;
cin >> start >> data >> next;
nodes[start].addr = start;
nodes[start].data = data;
nodes[start].next = next;
}
while (st != -){
v.push_back(nodes[st]);
st = nodes[st].next;
count++;
}
for (int i = ; i+k <= count; i = i + k){
reverse(v.begin() + i, v.begin() + i + k);
}
for (int i = ; i < count-; i++){
v[i].next = v[i + ].addr;
}
v[count-].next = -;
for (int i = ;i < count-; i++){
printf("%05d %d %05d\n", v[i].addr, v[i].data, v[i].next); }
printf("%05d %d %d\n", v[count - ].addr, v[count - ].data, v[count - ].next);
system("pause");
}

注意点:同B1025,链表题,都会要你重新排序输出,注意可以用vector实现链表,不要用静态数组,保存节点时一定要保存自己的地址。反转可以直接使用algorithm里的reverse

PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse的更多相关文章

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

  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. 【PAT甲级】1074 Reversing Linked List (25 分)

    题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的 ...

  4. PAT甲级1074 Reversing Linked List (25分)

    [程序思路] 先根据地址按顺序读入节点,入栈,当栈里的元素个数等于k时全部出栈,并按出栈顺序保存,最后若栈不为空,则全部出栈并按出栈的稀饭顺序保存,最后输出各节点 注意:输入的节点中有可能存在无用节点 ...

  5. PAT 1074. 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 ...

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

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

  8. PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)

    7-2 Block Reversing (25分)   Given a singly linked list L. Let us consider every K nodes as a block ( ...

  9. PAT 1074 Reversing Linked List[链表][一般]

    1074 Reversing Linked List (25)(25 分) Given a constant K and a singly linked list L, you are suppose ...

随机推荐

  1. Java中变量之局部变量、本类成员变量、父类成员变量的访问方法

    变量:局部变量.本类成员变量.父类成员变量 如何访问:如果变量名相同,则采用就近原则,哪个变量离所要调用的访问最近,那就么就输出,优先顺序为:局部变量 > 本类成员变量 > 父类成员变量 ...

  2. 在UWP中实现自己的MVVM设计模式

    其实写这篇博文的时候我是拒绝的,因为这牵扯到一个高大上的东西——"框架".一说起这个东西,很多朋友就感觉有点蒙了,尤其是编程新手.因为它不像在代码里面定义一个变量那么显而易见,它是 ...

  3. TCP握手过程中建连接的流程和队列

    这里有两个队列:syns queue(半连接队列):accept queue(全连接队列). 三次握手过程中: 第一步: server 收到 client 的 syn 后,把这个连接信息放到半连接队列 ...

  4. PHP7.27: connect mysql 5.7 using new mysqli_connect

    <!doctype html> <html> <head> <meta name="viewport" content="wid ...

  5. 2018-01-04 浅尝The Little Prover一书, 重逢Chez Scheme

    书开篇之前说, 本书的目标的一个例子: 证明(reverse (reverse x))对于任何列表x, 结果总是x. (安装Chez Scheme的200字请看最后) 书刚开始, 就用到一个schem ...

  6. Python3选择支持非ASCII码标识符的缘由

    原文在: PEP 3131 -- Supporting Non-ASCII Identifiers. Python2并不支持非ASCII码标识符. PEP的全称是Python Enhancement ...

  7. React 入门学习笔记整理(九)——路由

    (1)安装路由 React-router React-router提供了一些router的核心api,包括Router, Route, Switch等,但是它没有提供dom操作进行跳转的api. Re ...

  8. Android系统启动流程(三)解析SystemServer进程启动过程

    1.Zygote启动SystemServer进程 在上一篇文章中我们讲到在ZygoteInit.java的startSystemServer函数中启动了SyetemServer进程,如下所示. fra ...

  9. Java String和Date的转换

    String—>Date方法一: String dateString = "2016-01-08"; try { SimpleDateFormat sdf = new Sim ...

  10. Spring 事件

    JDK事件 java通过java.util.EventObject类和java.util.EventListener接口描述事件和监听器 事件源,事件的产生者,任何一个EventObject都必须拥有 ...