使用while循环复制堆栈内容的算法 [英] algorithm to copy content of a stack using a while loop

查看:171
本文介绍了使用while循环复制堆栈内容的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用while循环将堆栈s1的内容复制到s2的算法

algorithm to copy the content of stack s1 to s2 using a while loop

推荐答案

假设您只有push,pop,peek和IsEmpty操作,则需要两个循环和一个中间堆栈:
在第一个循环中,将每个项目从堆栈中弹出,然后将其推入临时堆栈.
在第二个循环中,将每个项目从临时堆栈中弹出,然后将其推到原始堆栈和输出堆栈上.

如果您可以从底部开始遍历,那么它不是堆栈复制操作,而是内存复制作业.
Assuming that you only have push, pop, peek and IsEmpty operations you need two loops, and an intermediate stack:
In the first loop, you pop each item off the stack, and push it onto the temporary stack.
In the second loop, you pop each item off the temporary stack and push it onto both the original stack and the output stack.

If you can traverse the start from the bottom, then it isn''t a stack copy operation, it''s just a memory copy job.


这篇关于使用while循环复制堆栈内容的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆