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
#include <iostream>
#include <vector>
#define M 100000
using namespace std;
struct ele{
int addr;
int data;
int next=-;
};
int main(){
int iniAddr,num,revNum,tmpAddr,tmpData,tmpNext;
ele element[M],last;
cin>>iniAddr>>num>>revNum;
while(num--){
cin>>tmpAddr>>tmpData>>tmpNext;
element[tmpAddr].addr=tmpAddr;
element[tmpAddr].data=tmpData;
element[tmpAddr].next=tmpNext;
}
vector<ele> vec;
while(iniAddr!=-){
vec.push_back(element[iniAddr]);
iniAddr=element[iniAddr].next;
}
//反转
int point=;bool start=true;
while(true){
point+=revNum;
if(point>vec.size()) break;
else{
for(int i=point-,j=;j<revNum;j++,i--){
if(start) printf("%05d ",vec[i].addr);
else printf("%05d\n%05d ",vec[i].addr,vec[i].addr);
printf("%d ",vec[i].data);
last=vec[i];
start=false;
}
}
}
point-=revNum;
for(int i=point;i<vec.size();i++){
if(start) printf("%05d ",vec[i].addr);
else printf("%05d\n%05d ",vec[i].addr,vec[i].addr);
printf("%d ",vec[i].data);
start=false;
last=vec[i];
}
printf("-1");
system("pause");
return ;
}

浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)的更多相关文章

  1. 浙大数据结构课后习题 练习二 7-3 Pop Sequence (25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  2. 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  3. 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  4. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  5. PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)

    PTA数据结构与算法题目集(中文)  7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...

  6. L2-004 这是二叉搜索树吗? (25 分) (树)

    链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070971912192 题目: 一棵二叉搜索树可被递归地定义为 ...

  7. 数据结构练习 02-线性结构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. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  9. 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总

    大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...

随机推荐

  1. java文件夹上传下载控件分享

    用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个需要在JAVA.MyEclipse环境下大文件上传的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助. 以下 ...

  2. Docker学习笔记 — Docker私有仓库搭建

    Docker学习笔记 — Docker私有仓库搭建   目录(?)[-] 环境准备 搭建私有仓库 测试 管理仓库中的镜像 查询 删除 Registry V2   和Mavan的管理一样,Dockers ...

  3. tween.js的API实践

    看了网上多篇关于tween的使用教程,基本上千篇一律,大多数的写法都是像下面这样: function initTween(geometry) { }; tween = }, ); tween.easi ...

  4. PHP进阶之路 -- 02 面向对象

    PHP进阶之路-- 之 “面向对象” 基础 概念 类的介绍和定义 类的属性 类常量 类的自动加载 构造函数和析构函数 访问控制 对象继承 范围解析操作符 static静态关键字 抽象类 对象接口 Tr ...

  5. Appium,AirTest切换使用时,appium罢工之坑(1)

    由于一些元素不能定位,所以最近项目引用了Airtest 问题场景: case的开始使用appium,case执行过程中,切换到airtest操作元素,待执行完airtest相关步骤之后,后续使用app ...

  6. 【VS开发】设备控制台 (DevCon.exe) 示例

    设备控制台 (DevCon.exe) 示例 本部分提供以下设备控制台 (DevCon.exe) 命令的示例: DevCon HwIDs 示例 1:查找所有硬件 ID 示例 2:使用模式查找硬件 ID ...

  7. Source 介绍

    !!!1.Avro Source 监听AVRO端口来接受来自外部AVRO客户端的事件流. 利用Avro Source可以实现多级流动.扇出流.扇入流等效果. 另外也可以接受通过flume提供的Avro ...

  8. WTForms常用的验证器

    from wtforms import Form,StringField,IntegerField from wtforms import validators from wtforms.valida ...

  9. Elasticsearch集群基本操作

    检查集群的命令 $ curl http://172.16.101.55:9200/_cat =^.^= /_cat/allocation /_cat/shards /_cat/shards/{inde ...

  10. round函数——银行家舍入算法

    在处理四舍五入时,相信大部分人会使用math.round函数(不同的语言应该都有).有没有考虑过,这个函数是不是自己所需要的? po主碰到的问题是用来计算平均分.有个顶真的学生反映,明明是86.5,怎 ...