题目介绍:如题,两个栈实现一个队列的功能
思路:栈是先进后出,两个栈一起用可以实现先进先出
java程序:
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
public int pop() {
int res;
if(stack2.empty())
while(!stack1.empty()){
stack2.push(stack1.pop());
}
res = stack2.pop();
return res;
}
}