import java.util.Stack;
public class Solution {
Stack<Integer> head = new Stack<Integer>();
Stack<Integer> tail = new Stack<Integer>();
public void push(int node) {
head.push(node);
}
public int pop() {
if(tail.empty()){
transfer();
}
return tail.pop();
}
public void transfer(){
while(!head.empty()){
tail.push(head.pop());
}
}
}
本文介绍了一种使用两个栈来实现队列的数据结构方法。通过将元素压入一个栈并在需要弹出时转移到另一个栈中,可以有效地模拟队列的先进先出特性。这种方法在不需要额外数据结构的情况下实现了队列的基本操作。
920

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



