package stackAndQueue;
import java.util.Stack;
public class TwoStacksQueue {
public static void main(String[] args) {
TwoStacksQueueReal twoStacksQueueReal = new TwoStacksQueueReal();
for (int i = 0; i < 10; i++) {
twoStacksQueueReal.add(i);
}
for (int i = 0; i < 10; i++) {
System.out.println(twoStacksQueueReal.poll());
}
}
}
class TwoStacksQueueReal {
private Stack<Integer> stackPush;
private Stack<Integer> stackPop;
public TwoStacksQueueReal() {
this.stackPop = new Stack<Integer>();
this.stackPush = new Stack<Integer>();
}
public void add(int newNum) {
this.stackPush.push(newNum);
}
public int poll() {
if (stackPush.isEmpty() && stackPop.isEmpty()) {
throw new RuntimeException("Your queue is empty");
} else if (stackPop.isEmpty()) {
while (!(stackPush.isEmpty())) {
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public int peek() {
if (stackPush.isEmpty() && stackPop.isEmpty()) {
throw new RuntimeException("Your queue is empty");
} else if (stackPop.isEmpty()) {
while (!(stackPush.isEmpty())) {
stackPop.push(stackPush.pop());
}
}
return stackPop.peek();
}
}
11-17