import java.util.*;
public class Solution {
/**两个栈实现队列*/
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
stack1.push(node);
while (!stack2.isEmpty()) {
stack1.push(stack2.pop());
}
}
public int pop() {
return stack1.pop();
}
}两个栈实现队列
最新推荐文章于 2024-09-17 14:27:29 发布
本文介绍了一种使用两个栈来实现队列的方法。通过不断在两个栈之间转移元素,可以达到队列先进先出的效果。具体实现包括了push和pop操作。
916

被折叠的 条评论
为什么被折叠?



