classMyQueue{privatefinalStack<Integer> inStack =newStack<>();privatefinalStack<Integer> outStack =newStack<>();/** Initialize your data structure here. */publicMyQueue(){}/** Push element x to the back of queue. */publicvoidpush(int x){if(outStack.empty()){
outStack.push(x);}else{while(!outStack.empty()){
inStack.push(outStack.pop());}
inStack.push(x);while(!inStack.empty()){
outStack.push(inStack.pop());}}}/** Removes the element from in front of queue and returns that element. */publicintpop(){return outStack.pop();}/** Get the front element. */publicintpeek(){return outStack.peek();}/** Returns whether the queue is empty. */publicbooleanempty(){return outStack.empty();}}/**
* 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 param_4 = obj.empty();
*/
数据结构和算法(六)–队列(Queue)什么是队列队列是一种特殊的线性表,只能在头尾进行操作队尾(rear):只能从队尾进行添加元素,一般叫做入队(enQueue)队头(front):智能从队头移除元素,一般叫做出队(deQueue)先进先出原则,First In First Out,FIFO[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dkb4opGj-1622453483056)(imgs/队列的数据结构.png)]队列的接口设计int size()