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. SQL学习(三)Select语句:返回前多少行数据

    在实际工作中,我们可能根据某种排序后,只需要显示前多少条数据,此时就需要根据不同的数据库,使用不同的关键字 一.SQL Server/Access select top 数量/百分比 from tab ...

  2. 《Improving Deep Neural Networks:Hyperparameter tuning, Regularization and Optimization》课堂笔记

    Lesson 2 Improving Deep Neural Networks:Hyperparameter tuning, Regularization and Optimization 这篇文章其 ...

  3. 自在因梦 | 威爾伯的Fourth Turning所引發的聯想

    2015-05-06                                                 胡因梦                                       ...

  4. CnPack 开源软件项目

    Cnpack公共窗体库 ------------------------------ CnPack 2009-09-14 SVN 包,包括以下内容: 1. CnPack 组件包所有源代码.2. CnP ...

  5. Kafka-manager安装部署

    一.kafka-manager 简介 为了简化开发者和服务工程师维护Kafka集群的工作,yahoo构建了一个叫做Kafka管理器的基于Web工具,叫做 Kafka Manager.这个管理工具可以很 ...

  6. SQLServer 断开数据库连接

    数据库名:test1 1. 查询数据库当前连接 select * from master.sys.sysprocesses where dbid = db_id('test1') 2. 断开指定连接 ...

  7. 【HANA系列】SAP HANA SQL计算某日期是当年的第几天

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA SQL计算某日 ...

  8. Nginx实现负载均衡的方式有哪几种呢?

    什么是负载均衡 当一台服务器的单位时间内的访问量越大时,服务器压力就越大,大到超过自身承受能力时,服务器就会崩溃.为了避免服务器崩溃,让用户有更好的体验,我们通过负载均衡的方式来分担服务器压力. 我们 ...

  9. MySQLdb._exceptions.OperationalError: (2059, <NULL>)

    这是将 将数据迁移至mysql8.0时遇到的问题, 在网上找到了解决方案(亲测有用), 这是因为mysql8.0密码加密的问题,mysql8.0对用户密码的加密方式为caching_sha2_pass ...

  10. word2010 标题自动编号设置

    今天打算写篇文档,发现生成标题时无法自动生成编号,上网查了一下,现在把解决办法跟附图一块儿奉上. 新建word文档: 默认版式: 设置自动编号所在工具栏位置: 设置选择: 最终结果: