import java.util.Stack;
/**
* Created by Administrator on 17.6.2.
*/
public class TwoStacksQueue {
public Stack<Integer> stackPush;
public Stack<Integer> stackPop;
public TwoStacksQueue() {
stackPush = new Stack<Integer>();
stackPop = new Stack<Integer>();
}
public void add(int num) {
stackPush.push(num);
}
public int pop() {
if (stackPush.empty() && stackPop.empty()) {
throw new RuntimeException("empty");
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public int peek() {
if (stackPush.empty() && stackPop.empty()) {
throw new RuntimeException("empty");
} else if (stackPop.empty()) {
while (!stackPush.empty()) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
网友评论