zoukankan      html  css  js  c++  java
  • Implement Stack using Queues

    Implement Stack using Queues

    问题:

    Implement the following operations of a stack using queues.

    • push(x) -- Push element x onto stack.
    • pop() -- Removes the element on top of the stack.
    • top() -- Get the top element.
    • empty() -- Return whether the stack is empty.

    思路:

      使用两个队列来回交替的来pop

    我的代码:

    class MyStack {
        Queue<Integer> one = new LinkedList<Integer>();
        Queue<Integer> two = new LinkedList<Integer>();
        public void push(int x) {
            if(one.isEmpty())
                two.offer(x);
            else
                one.offer(x);
        }
    
        // Removes the element on top of the stack.
        public void pop() {
            if(one.isEmpty())
            {
                while(two.size() != 1)
                    one.offer(two.poll());
                two.poll();
            }
            else
            {
                while(one.size() != 1)
                    two.offer(one.poll());
                one.poll();
            }
        }
        // Get the top element.
        public int top() {
            if(one.isEmpty())
            {
                while(two.size() != 1)
                    one.offer(two.poll());
                int last = two.poll();
                one.offer(last);
                return last;
            }
            else
            {
                while(one.size() != 1)
                    two.offer(one.poll());
                int last = one.poll();
                two.offer(last);
                return last;
            }
        }
    
        // Return whether the stack is empty.
        public boolean empty() {
            return one.isEmpty() && two.isEmpty();
        }
    }
    View Code

    他人代码:

    class MyStack {
        Queue<Integer> q = new LinkedList<Integer>();
    
        // Push element x onto stack.
        public void push(int x) {
            q.add(x);
        }
    
        // Removes the element on top of the stack.
        public void pop() {
            int size = q.size();
            for(int i = 1; i < size; i++)
                q.add(q.remove());
            q.remove();
        }
    
        // Get the top element.
        public int top() {
            int size = q.size();
            for(int i = 1; i < size; i++)
                q.add(q.remove());
            int ret = q.remove();
            q.add(ret);
            return ret;
        }
    
        // Return whether the stack is empty.
        public boolean empty() {
            return q.isEmpty();        
        }
    }
    View Code

    学习之处:

    • 别人用了一个队列就解决了问题,我用了两个队列,浪费了空间,别人代码的精华之处在于for(int i = 1; i < size; i++) q.add(q.remove());通过size,重新将队尾的元素拿出来加到后面。
  • 相关阅读:
    JVM学习笔记(三)------内存管理和垃圾回收【转】
    JVM学习笔记(二)------Java代码编译和执行的整个过程【转】
    JVM学习笔记(一)------基本结构【转】
    Java程序编译和运行的过程【转】
    linux C判断文件是否存在【转】
    Java编译那些事儿【转】
    CTSC1999补丁VS错误题解
    ASP.NET MVC学前篇之Ninject的初步了解
    setSingleChoiceItems和setPositiveButton两者触发时期
    B. Sereja and Mirroring
  • 原文地址:https://www.cnblogs.com/sunshisonghit/p/4580936.html
Copyright © 2011-2022 走看看