package com.example.demo02;
import java.util.Stack;
public class StackToQueue {
private Stack<Integer> pushStack;
private Stack<Integer> popStack;
public StackToQueue(){
pushStack=new Stack<>();
popStack=new Stack<>();
}
public void pushToPop(){
if (popStack.isEmpty()){
while (pushStack.isEmpty()){
popStack.push(pushStack.pop());
}
}
}
public void add(int value){
pushStack.push(value);
}
public int poll(){
if (popStack.isEmpty()&&pushStack.isEmpty()){
throw new RuntimeException("Stack is empty");
}
pushToPop();
return popStack.pop();
}
public int peek(){
if (popStack.isEmpty()&&pushStack.isEmpty()){
throw new RuntimeException("Stack is empty");
}
pushToPop();
return popStack.peek();
}
}