当前位置:网站首页>Reverse a stack with recursive functions and stack operations only
Reverse a stack with recursive functions and stack operations only
2022-06-28 13:49:00 【Hua Weiyun】
Reverse a stack with recursive functions and stack operations only
【 subject 】
A stack is pushed in turn 1、2、3、4、5, So from the top to the bottom of the stack are 5、4、3、2、1. After transposing this stack , From the top to the bottom of the stack is 1、2、3、4、5, That is to realize the reverse order of the elements in the stack , But it can only be implemented with recursive functions , You can't use other data structures .
【 Ideas 】
Only recursive functions are required , You need to design two functions , Function returns the bottom of the stack element and removes ; Function 2 implements the reverse stack , Function one is required .
explain : The picture comes from Zuoshen's program code interview guide , For learning purposes only .
Function one :
// Return to the bottom of the stack and remove public static int getAndRemoveLastElement(Stack<Integer> stack){ int res = stack.pop(); if(stack.isEmpty()){ return res; }else{ int last = getAndRemoveLastElement(stack); stack.push(res); return last; }}
Function two :
// Reverse order stack public static void reverse(Stack<Integer> stack){ if(stack.isEmpty()){ return ; } int i = getAndRemoveLastElement(stack); reverse(stack); stack.push(i);}
【 Code 】
package keafmd.accumulate.codeinterviewguide.reversestack;import java.util.Stack;/** * Keafmd * * @ClassName: ReverseStack * @Description: Use function reverse stack * @author: Cowherd Conan * @date: 2022-06-22 11:01 */public class ReverseStack { // Return to the bottom of the stack and remove public static int getAndRemoveLastElement(Stack<Integer> stack){ int res = stack.pop(); if(stack.isEmpty()){ return res; }else{ int last = getAndRemoveLastElement(stack); stack.push(res); return last; } } // Reverse order stack public static void reverse(Stack<Integer> stack){ if(stack.isEmpty()){ return ; } int i = getAndRemoveLastElement(stack); reverse(stack); stack.push(i); } public static void main(String[] args) { Stack<Integer> stack = new Stack<>(); for(int i=1;i<=5;i++){ stack.push(i); } System.out.print(" Before reverse order :"); while (!stack.isEmpty()){ System.out.print(stack.pop()+" "); } System.out.println(); for(int i=1;i<=5;i++){ stack.push(i); } reverse(stack); System.out.print(" After reverse order :"); while (!stack.isEmpty()){ System.out.print(stack.pop()+" "); } System.out.println(); }}
effect :
Before reverse order :5 4 3 2 1 After reverse order :1 2 3 4 5 Process finished with exit code 0
边栏推荐
- 初识exception
- Pytorch main modules
- Yii2 connects to websocket service to realize that the server actively pushes messages to the client
- Kubernetes 深入理解Kubernetes(二) 声明组织对象
- 你的代码会说话吗?(上)
- New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta
- 求解汉诺塔问题
- [机缘参悟-32]:鬼谷子-抵巇[xī]篇-面对危险与问题的五种态度
- 众昂矿业着眼氟化工产业,布局新能源产业链
- Go array and slice, []byte to string[easy to understand]
猜你喜欢
开源社邀您参加OpenInfra Days China 2022,议题征集进行中~
3、项目的整体UI架构
Action interprets value. The chairman of chenglian Youpin Han attended the Guangdong Yingde flood fighting donation public welfare event
Arcgis 矢量中心点生成矩形并裁剪tif图像进行深度学习样本训练
Can your code talk? (upper)
First knowledge of exception
APP冷热启动专项测试
Websocket automatically disconnects in 1 minute
Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain
一个bug肝一周...忍不住提了issue
随机推荐
Other domestic mobile phones failed to fill the vacancy of Huawei, and apple has no rival in the high-end mobile phone market
2022年中国运维安全产品市场规模及发展趋势预测分析
你的代码会说话吗?(上)
How to open an account on the flush? Is it safe
Kubernetes in-depth understanding of kubernetes (I)
New product experience: Alibaba cloud's new generation of local SSD instance I4 open beta
DevEco Studio 3.0编辑器配置技巧篇
行动诠释价值,城联优品韩董事长出席广东英德抗洪捐赠公益活动会
Class structure in C language - dot
NFT digital collection system development (3D modeling economic model development case)
iNFTnews | 科技巨头加快进军Web3和元宇宙
Navicat Premium 16 永久破解激活工具及安装教程(亲测可用)
Black apple installation tutorial OC boot "suggestions collection"
恒生电子:金融分布式数据库LightDB通过中国信通院多项测评
Is it safe for Huatai Securities to open an account? Is there any risk in opening an account
再谈exception——异常抛出时会发生什么?
Prediction of red wine quality by decision tree
(original) [Maui] realize "floating action button" step by step
How to set auto format after saving code in vscade
Yii2 connects to websocket service to realize that the server actively pushes messages to the client