package com.chanmufeng.codingInterviewGuide.stackAndQueue_10;
import java.util.Stack;
/**
* 用两个栈实现队列功能
*/
public class StackQueue {
private static Stack<Integer> pushStack;
private static Stack<Integer> popStack;
public StackQueue() {
pushStack = new Stack<>();
popStack = new Stack<>();
}
public static void enqueue(int newNum) {
pushStack.push(newNum);
}
public static int poll() {
if (pushStack.isEmpty() && popStack.isEmpty()) {
throw new RuntimeException("queue is empty");
} else if (popStack.isEmpty()) {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
}
}
return popStack.pop();
}
public static int peek() {
if (pushStack.isEmpty() && popStack.isEmpty()) {
throw new RuntimeException("queue is empty");
} else if (popStack.isEmpty()) {
while (!pushStack.isEmpty()) {
popStack.push(pushStack.pop());
}
}
return popStack.peek();
}
public static void main(String[] args) {
}
}