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
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100100;
struct Node{int address,data,next;int order;
}node[maxn];bool cmp(Node a,Node b){return a.order < b.order;
}
int main(){int i,address;for(i = 0; i < maxn; i++){node[i].order = maxn;}int begin,n,k;  //起始节点地址,节点数目,分组数 scanf("%d%d%d",&begin,&n,&k);for(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;while(p != -1){node[p].order = count++;p = node[p].next;}sort(node,node+maxn,cmp);n = count; //因为count=0占一个有效节点,退出循环时,count值就是有效节点 for(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);}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) printf("-1\n");  //刚好除整 else{   //如果最后一个节点不规则 printf("%05d\n",node[(i+1)*k].address);for(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");}// else} // for(i)}//else}//else} //for(i)return 0;
}

转载于:https://www.cnblogs.com/wanghao-boke/p/8550233.html

1074. Reversing Linked List (25)相关推荐

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

  2. 1074. Reversing Linked List (25)-PAT甲级真题

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

  3. 1074 Reversing Linked List (25 分) java 题解

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

  4. 1074 Reversing Linked List (25 分)【难度: 一般 / 知识点: 链表】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805394512134144 乙级里面的原题吧,就用哈希表建链表,reve ...

  5. PAT 1074 Reversing Linked List

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

  6. PAT甲级1074 Reversing Linked List :[C++题解]反转链表,借用vector

    文章目录 题目分析 题目链接 题目分析 分析:数组模拟链表,这题反转操作在数组中进行,然后直接输出即可,甚至不用放回到链表. //遍历链表,该链表用数组模拟 //保存链表结点地址到数组中 for(in ...

  7. 每隔k次反转一次 链表_PTA 5-2 Reversing Linked List (25) [法一] - 线性表 - 链表反转 (PAT 1074)...

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

  8. PAT-A 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 ...

  9. 02-线性结构3 Reversing Linked List (25 分)

    如果 单纯从做题角度来讲,上一个做法当然没问题,可是目的是为了练习静态链表,so还是不要投机取巧了,静态链表解法如下: #include <stdio.h> struct data {in ...

最新文章

  1. 关于学外语,这些人人都知道的“常识”,可能是错的……
  2. linux CentOS6.x 修改主机名(Hostname)
  3. 序列化以及反序列化二叉树
  4. skimage库需要依赖 numpy+mkl 和scipy
  5. Android 最简单的MVP案例;
  6. OpenStack实践系列②认证服务Keystone
  7. 利用BI搭建零售业数据信息平台
  8. php 5.5编译安装教程,CentOS 5.5编译安装Nginx1.0.15+MySQL5.5.23+PHP5.3.10
  9. Newtonsoft.Json 序列化和反序列化 以及时间格式 2
  10. 谈谈R中的乱码(一)
  11. SVM支持向量机-——希尔伯特空间解释
  12. 加密狗登录PHP开发,C# 使用加密狗登录 示例源码
  13. 二元隐函数求二阶偏导_隐函数求二阶偏导
  14. Python 阿拉伯数字转换成英文
  15. 动态规划背包问题matlab,动态规划解决01背包问题
  16. rdkit 化学反应ReactionFromSmarts
  17. 主题黑板.html,黑板报主题
  18. 灭火机器人C语言程序,- 一款基于STM32的智能灭火机器人设计
  19. C++编译器优化:Copy Elision(省略不必要的拷贝)
  20. 电阻-横向评测(转载)

热门文章

  1. javascript 高级特性探讨A4-A5(call和原型.对象复制)
  2. iBatis入门和开发环境搭建
  3. 在ne中分析贱谈.net中简体转繁体
  4. ManualResetEvent用法
  5. r语言转化为python_数值型与字符型转换总结|R语言
  6. mysql生产环境加索引_【生产篇】_MySQL环境下如何查看基于表的索引定义
  7. NoSQLBooster for MongoDB 中跨库关联查询
  8. 160 - 48 DueList.3
  9. 《DBNotes:single_table访问方法、MRR多范围读取优化、索引合并》
  10. ruby 怎么抛异常_Ruby中的异常处理