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. Eureka 2.0 闭源--选择Consul???[转]

    原文链接: https://www.cnblogs.com/williamjie/p/9369800.html 在上个月我们知道 Eureka 2.0 闭源了,但其实对国内的用户影响甚小,一方面国内大 ...

  2. iOS使用UIBezierPath实现ProgressView

    实现效果如下: 界面采用UITableView和TabelViewCell的实现,红色的视图采用UIBezierPath绘制.注意红色的部分左上角,左下角是直角哟!!!!不多说<这里才是用UIB ...

  3. 自然数幂和(递推式k^2方法)

    先用这个方法顶一下!

  4. Longest Palindromic Subsequence

    Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...

  5. hive自定义udaf函数

    自定义udaf函数的代码框架 //首先继承一个类AbstractGenericUDAFResolver,然后实现里面的getevaluate方法 public GenericUDAFEvaluator ...

  6. redis缓存雪崩

    缓存雪崩 缓存雪崩,是指在某一个时间段,缓存集中过期失效. 产生雪崩的原因之一,比如在写本文的时候,马上就要到双十二零点,很快就会迎来一波抢购,这波商品时间比较集中的放入了缓存,假设缓存一个小时.那么 ...

  7. 单页面应用 之 项目中集成插件vue-router

    \es6\my-complex-project>npm install  vue-router -S    (S 表示这个包下载到,当前的项目中) 导入写好的  router 这里尽量使用  @ ...

  8. 升级降级(期望DP)2019 Multi-University Training Contest 7 hdu杭电多校第7场(Kejin Player)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6656 题意: 有 1~n 个等级,你现在是1级,求升到n级的花费期望.会给你n个条件(i~i+1级升级 ...

  9. qq 面对面传文件,应用

    使用方式:打开qq,点击右上角里面的面对面传 传输内容:应用,文件 好处:不耗流量,快速

  10. Web前端开发HTML基础

    HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记),相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器根据标 ...