stack1永远做栈压入,永远先从栈2拿数据,栈2空了再从栈1拿,保证顺序性
public class StockToQueue {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
}
/**
* 永远从非空的栈2先拿数据
*/
public int pop() {
if (stack1.empty()&&stack2.empty()){
throw new RuntimeException("queue is empty");
}
if (stack2.empty()){
while (!stack1.empty()){
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
}