当前位置:网站首页>如何仅用递归函数和栈操作逆序一个栈
如何仅用递归函数和栈操作逆序一个栈
2022-07-25 07:38:00 【dlz456】
题目:一个栈依次压入1,2,3,4,5,那么从栈顶到栈底分别为:5,4,3,2,1。将这个栈转置后,从栈顶到栈底为1,2,3,4,5,也就是实现栈中元素的逆序,但是只能用递归函数来实现,不能用其他数据结构。
我自己的理解:

如上图,将左边的栈通过逆序转换为右边的栈,右边的栈最下面是3,因此最先要把3压进去,也就说明在从左边的栈中提取元素时,要最后提取到三,因此就说明是从栈底元素开始到栈顶元素依次进行遍历。但是对于左边来说,3在栈顶,1在栈底,和我们想要的顺序不同,因此这里还需要写一个递归。对于右边的栈来说,在压入第一个元素3的时候,要求栈为空,也就是意味着我们在对初始的左边栈进行数据提取时,应该采用pop而不是peek。
代码实现如下:
package text3;
import java.util.Stack;
public class class1 {
public class1() {
}
public static int getlast(Stack<Integer> stack) {
int result=stack.pop();
if(stack.empty()) {
return result;
}else {
int last=getlast(stack);
stack.push(result);
return last;
}
}
public static void reverse(Stack<Integer> stack) {
if(stack.empty()) {
return;
}
int a=getlast(stack);
reverse(stack);
stack.push(a);
}
}
边栏推荐
- When importing data in batches, you always prompt "failure reason: SQL parsing failure: parsing file failure:: null". What's the matter?
- Introduction to Manhattan distance
- Servlet常用类剖析
- P1049 [NOIP2001 普及组 T4] 装箱问题
- GBase 8a中关于--skip-networking 问题
- [pytorch] the most common function of view
- 10 key points and 5 measures for good project management
- Tips - prevent system problems and file loss
- [unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
- [paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
猜你喜欢

Line generation (matrix ')
![[paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized](/img/a8/c5d14ec5f4a22f7925c6d7454b5a58.png)
[paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized

PADS导出gerber文件
![[paper notes] next vit: next generation vision transformer for efficient deployment in real industry](/img/ea/56881999a90f9c65f5f8768f9574bd.png)
[paper notes] next vit: next generation vision transformer for efficient deployment in real industry

JS cannot get content disposition in headers

How to use network installation to deploy multiple virtual servers in KVM environment
![[unity introduction program] basic concepts - 2D collider collider 2D](/img/cf/a546238a5eaf4707006ecf1b7f19c6.png)
[unity introduction program] basic concepts - 2D collider collider 2D

【Unity入门计划】基本概念-2D碰撞体Collider 2D

深度学习训练和测试时出现问题:error: the following arguments are required: --dataroot,解决:训练文件的配置方法和测试文件的配置方法

Use cyclegan to train self-made data sets, popular tutorials, and get started quickly
随机推荐
[paper notes] progressive layered extraction (PLE): a novel multi task learning (MTL) model for personalized
PADS导出gerber文件
9 best engineering construction project management systems
[ES6] function parameters, symbol data types, iterators and generators
Nailing the latest version, how to clear the login phone number history data
2-6.自动化采集
cesium简介
Elasticserach里delete_by_query的机制是什么?
Google Earth engine - Landsat 1985-2020 ecological remote sensing index resi calculation
Huawei wireless device sta black and white list configuration command
nanodet训练时出现问题:ModuleNotFoundError: No module named ‘nanodet‘的解决方法
Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
Delete in elasticserach_ by_ What is the mechanism of query?
Lidar construction map (overlay grid construction map)
P1046 [NOIP2005 普及组 T1] 陶陶摘苹果
Nano data, football data, football match scores, sports data API, Qatar world cup
Growth path - InfoQ video experience notes [easy to understand]
Bingbing's learning notes: classes and objects (Part 1)
What are the types of financial products in 2022? Which is suitable for beginners?
New version 8.6 SEO quick release system (can be built at source level)