import java.util.Stack;
//要用两个栈
public class MyQueueWithStack {
Stack<Integer> stack1=new Stack<>();
Stack<Integer> stack2=new Stack<>();
//stack1管进,stack2管出
public void push(int element){
//尾插
stack1.push(element);
}
public int pop(){
//删除栈顶元素,并返回
if(stack2.empty()){
while(!stack1.empty())
stack2.push(stack1.pop());
}
//不可能栈1栈2都为空,如果为空,这个类的对象对象在调用这个pop时就会出错
//能调用该方法的对象肯定不会为空,为空就不能调用该方法
return stack2.pop();
}
public int peek(){
//查看栈顶元素
if(stack2.empty()){
while(!stack1.empty())
stack2.push(stack1.pop());
}
return stack2.peek();
}
public boolean empty(){
return stack1.empty()&&stack2.empty();
}
}