队列实现栈

class MyStack{
        private Queue<Integer> queue = new LinkedList<>();
        
        public void push(int x){
            queue.add(x);
            int cnt = queue.size();
            while(cnt-->1){
                queue.add(queue.poll());
            }
        }
        
        public int pop(){
            return queue.remove();
        }
        
        public int top(){
            return queue.peek();
        }
        
        public boolean empty(){
            return queue.isEmpty();
        }
    }

 

posted @ 2020-08-06 14:51  helloworldmybokeyuan  阅读(70)  评论(0编辑  收藏  举报