浙大数据结构课后习题 练习二 7-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 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 分)的更多相关文章
- 浙大数据结构课后习题 练习二 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 ...
- 浙大数据结构课后习题 练习三 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 ...
- 浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)
PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分) Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...
- PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)
PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
- L2-004 这是二叉搜索树吗? (25 分) (树)
链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805070971912192 题目: 一棵二叉搜索树可被递归地定义为 ...
- 数据结构练习 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 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总
大家好,我是Mac Jiang,非常高兴您能在百忙之中阅读我的博客!这个专题我主要讲的是Coursera-台湾大学-機器學習基石(Machine Learning Foundations)的课后习题解 ...
随机推荐
- sql(存储过程,事务,索引,游标,触发器)
1.SqlServer中like '%_%'来匹配下划线: --在sql server的like中下划线类似于通配符%,所以无法使用like '%_%'来匹配下划线 select * from cla ...
- java:redis(redis安装配置,redis的伪集群配置)
1.redis安装配置: .安装gcc : yum install gcc-c++ .使用FTP工具FileZilla上传redis安装包到linux根目录下(当前步骤可以替换为:在root目录下执行 ...
- Sqlserver实现故障转移 — AlwaysOn实现故障转移(4)
目的:在已经加域的计算机上安装sqlserver2012,并配置系统级故障转移及数据库,实现AlwayOn. 域控的建立详见:https://www.cnblogs.com/xiaoerlang90/ ...
- 不可不知的JavaScript - 闭包函数
闭包函数 什么是闭包函数? 闭包函数是一种函数的使用方式,最常见的如下: function fn1(){ function fn(){ } return fn; } 这种函数的嵌套方式就是闭包函数,这 ...
- ubuntu下自动获取ip设置
vi /etc/network/interfaces文件为如下内容 wq保存 重启网卡:sudo /etc/init.d/networking restart
- 前端 api 请求缓存方案
参考链接:https://blog.csdn.net/zhuoganliwanjin/article/details/89598753#commentBox
- SolidWorks学习笔记6抽壳,加强筋,扫描,放样
抽壳 概念:移除一个或者多个面,然后将其余的模型外表面向内或者向外偏移相等或者不等的距离 针对不同面设置不同厚度 方向参考 有实体的一侧是内测, 没有实体的一侧是外侧 顺序 先圆角再抽壳 加强筋. 点 ...
- 多线程--原子操作 Interlocked系列函数
[转]原文地址:http://blog.csdn.net/morewindows/article/details/7429155 线程同步与互斥: 互斥主要指多个线程不能同时访问一个资源,如打印机就是 ...
- iframe在firefox下的无法正常target问题
今日在改一个网站的时候碰到个奇葩的问题. 把网站按照结构划分,做了个主页面,嵌入iframe,设定该iframe的name,主页面上的链接使用target保证在iframe中打开该链接. 听上去很常规 ...
- Python input/output boilerplate for competitive programming
The following code is my submission for Codeforces 1244C The Football Season. import io import sys i ...