用两个栈来实现一个队列
2017-08-31 09:02
218 查看
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。import java.util.Stack; public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { while(!stack2.isEmpty()) { Integer pop = stack2.pop(); stack1.push(pop); } stack1.push(node); } public int pop() { while(!stack1.isEmpty()) { Integer pop = stack1.pop(); stack2.push(pop); } return stack2.pop(); } }
相关文章推荐
- 算法导论10.1-6习题解答(用两个栈实现一个队列)
- 【数据结构】栈以及两个栈实现一个队列
- 用两个栈实现一个队列
- c++实现两个堆栈模拟一个队列
- Stack_Queue 两个栈实现一个队列 @CareerCup
- 面试题7_使用两个栈实现一个队列的功能
- 《剑指 Offer》学习(2)—— 9_两个栈实现一个队列
- 如何用两个栈实现一个队列
- 数据结构-使用两个栈实现一个队列
- 两个栈实现一个队列,两个队列实现一个栈
- 两个栈实现一个队列-思路
- 用两个栈实现一个队列
- 用两个栈实现一个队列:实现出队列和入队列功能,用两个队列实现一个栈
- 用两个栈实现一个队列
- 两个队列实现一个栈(两个队列循环)
- 两个队列实现一个栈 / 两个栈实现一个队列
- 【数据结构】两个栈实现一个队列
- 使用两个栈实现一个队列
- 【栈队列】一个数组实现两个栈(共享栈)
- 用两个栈实现一个队列