代码实现
class MyQueue {
Stack<Integer> stackin;//操作入栈
Stack<Integer> stackout;//操作出栈
public MyQueue() {
stackin = new Stack<>();
stackout= new Stack<>();
}
public void push(int x) {
stackin.push(x);
}
public int pop() {
dumpstakin();
return stackout.pop();
}
public int peek() {
dumpstakin();
return stackout.peek();
}
public boolean empty() {
return stackout.empty()&&stackin.empty();
}
public void dumpstakin()
{
if(!stackout.empty())
{
return ;
}
else
{
while(!stackin.empty())
{
stackout.push(stackin.pop());
}
}
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean p
学习如逆水行舟,不进则退。和小吴一起加油吧!