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 (≤) which is the total number of nodes, and a positive K (≤) 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

算法设计:
由于所给的地址是5位非负整数,可以定义两个维度为100005的一维数组data、Next,负责储存数据域和下一个地址
定义一个vector<int>listAddress,由所给链表开始地址处开始遍历整个链表,按遍历顺序将各结点地址储存到listAddress中。
按要求对listAddress数组进行翻转,可以利用c语言库里的reverse函数

按格式要求进行结果输出

注意点:

(1)题目给出的结点中可能有不在链表中的无效结点

(2)翻转时要注意如果最后一组要翻转的结点数量小于K,则不进行翻转;如果等于K,需要进行翻转

(3)输出时结点地址除-1外要有5位数字,不够则在高位补0 。所以地址-1要进行特判输出

 //s首先说明一下PAT的常见陷阱,就是给出的数据未必是一条链表,可能是多条,
//所以首先从输入的数据的中找到那条链表
//巨简单,使用vector反转就行了
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int head, N, K;
int nodes[], nexts[];
vector<int>list;
int main()
{
cin >> head >> N >> K;
int adrr, data, next, temp = head;
for (int i = ; i < N; ++i)
{
cin >> adrr >> data >> next;
nodes[adrr] = data;
nexts[adrr] = next;
}
while (temp != -)//找出这条链表
{
list.push_back(temp);
temp = nexts[temp];
}
for (int i = K; i <= list.size(); i += K)
reverse(list.begin() + i - K, list.begin() + i);
for (int i = ; i < list.size(); ++i)
{
printf("%05d %d ", list[i], nodes[list[i]]);
if (i < list.size() - )
printf("%05d\n", list[i+]);
else
printf("-1\n");
}
return ;
}

PAT甲级——A1074 Reversing Linked List的更多相关文章

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

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

  3. PAT A1074 Reversing Linked List (25 分)——链表,vector,stl里的reverse

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  4. A1074. Reversing Linked List

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  5. PAT Advanced 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 e ...

  6. 【PAT甲级】1052 Linked List Sorting (25 分)

    题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组 ...

  7. PAT_A1074#Reversing Linked List

    Source: PAT A1074 Reversing Linked List (25 分) Description: Given a constant K and a singly linked l ...

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

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

  9. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

随机推荐

  1. _IRP struct

    Windows XP x86 +0x000 Type : Int2B +0x002 Size : Uint2B +0x004 MdlAddress : Ptr32 _MDL +0x000 Next : ...

  2. 2008年最佳Web设计/前端开发技巧、脚本及资源总结

    工具&Web应用 13个可能会让你说”Thank You”的必不可少的开源应用 14个免费工具让你了解为什么人们会放弃访问你的网站 40+CSS生成器 74个我们可能已经忘记的适合网页设计师的 ...

  3. vue组件的props

    刚开始学习vue组件的时候经常被 props这个传值搞晕,做个笔记 Vue.component('item', { template: '#item-template', props: { model ...

  4. Sublime Text最舒服的主题

    Theme - Afterglow (官网链接) 贴上 preferences -> settings 里面的配置 { "theme": "Afterglow-gr ...

  5. Mysql: [Warning] Using a password on the command line interface can be insecure

    mysql: [Warning] Using a password on the command line interface can be insecure MySQL 5.6 警告信息 comma ...

  6. 网络攻击之代理IP

    1.通过代理IP的好处: (1)隐藏自己真实上网地址 (2)突破宽带商访问限制

  7. CSS——before和after伪元素

    之所以被称为伪元素,是因为他们不是真正的页面元素,html没有对应的元素,但是其所有用法和表现行为与真正的页面元素一样,可以对其使用诸如页面元素一样的css样式,表面上看上去貌似是页面的某些元素来展现 ...

  8. 二分查找总结及部分Lintcode题目分析 3

    Search in rotated sorted array,题目中也给出了相应的例子,就是在sorted array某个节点发生了翻转(ie.0 1 2 4 5 6 7 might become 4 ...

  9. 云-腾讯云-笔记:pom.xml 配置

    ylbtech-云-腾讯云-笔记:pom.xml 配置 1. pom.xml返回顶部 1.1 com.qcloud / 腾讯云 <!-- https://mvnrepository.com/ar ...

  10. File、FileFilter、递归初步

    java.io.File 文件和目录 路径名的抽象表示形式 文件:File 存储数据的 目录:Directory 文件夹 用来存储文件 路径:Path 定位具有平台无关性 在任意平台都可以使用 Fil ...