栈实现队列

class MyQueue{
        private Stack<Integer> in = new Stack<>();
        private Stack<Integer> out = new Stack<>();
        
        public void push(int x){
            in.push(x);
        }
        
        public int pop(){
            if(out.isEmpty()){
                while(!in.isEmpty()){
                    out.push(in.pop());
                }
            }
            return out.pop();
        }
        
        public int peek(){
            if(out.isEmpty()){
                while(!in.isEmpty()){
                    out.push(in.pop());
                }
            }
            return out.peek();
        }
        
        public boolean empty(){
            return in.isEmpty() && out.isEmpty();
        }
    }
原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/13446306.html