Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

思路:用两个栈实现队列 #5

Open
YZcxy opened this issue Apr 2, 2019 · 0 comments
Open

思路:用两个栈实现队列 #5

YZcxy opened this issue Apr 2, 2019 · 0 comments

Comments

@YZcxy
Copy link
Owner

YZcxy commented Apr 2, 2019

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

思路

队列是先进先出,而栈呢,是先进的后出,但是用两个栈,把第一个栈的值压入第二个栈,先进的第一个栈的值就跑到了第二个栈的最顶端了。 由此就可以模拟出先进先出的效果。
例如,所有push操作都往stack1里面存放,当有pop操作的时候,优先考虑stack2是否有值,有的话直接pop,没有就将stack1中的值压入stack2中,然后pop。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant