classMyQueue{
Deque<Integer> instack;
Deque<Integer> outstack;/** Initialize your data structure here. */publicMyQueue(){
instack =newLinkedList<>();
outstack =newLinkedList<>();}/** Push element x to the back of queue. */publicvoidpush(int x){
instack.push(x);}/** Removes the element from in front of queue and returns that element. */publicintpop(){if(outstack.isEmpty()){in2out();}return outstack.pop();}/** Get the front element. */publicintpeek(){if(outstack.isEmpty()){in2out();}return outstack.peek();}/** Returns whether the queue is empty. */publicbooleanempty(){return instack.isEmpty()&& outstack.isEmpty();}publicvoidin2out(){while(!instack.isEmpty()){
outstack.push(instack.pop());}}}
用栈实现队列class MyQueue { Deque<Integer> instack; Deque<Integer> outstack; /** Initialize your data structure here. */ public MyQueue() { instack = new LinkedList<>(); outstack = new LinkedList<>();