用队列实现栈(leetcode 225)

一:解题思路

用2个队列实现栈

二:完整代码示例 (C++、Java、Python)

C++:

class MyStack 
{
private:
    queue<int> m_queueIn;
    queue<int> m_queueOut;

    queue<int>* m_pIn;
    queue<int>* m_pOut;
public:
    MyStack() 
    {
        m_pIn = &m_queueIn;
        m_pOut = &m_queueOut;
    }

    void swap()
    {
        queue<int>* temp = m_pIn;
        m_pIn = m_pOut;
        m_pOut = temp;
    }

    void transfer()
    {
        while (m_pIn->size() > 1)
        {
            m_pOut->push(m_pIn->front());
            m_pIn->pop();
        }
    }

    /** Push element x onto stack. */
    void push(int x) 
    {
        m_pIn->push(x);
    }

    /** Removes the element on top of the stack and returns that element. */
    int pop() 
    {
        transfer();

        int ret = m_pIn->front();
        m_pIn->pop();

        swap();

        return ret;
    }

    /** Get the top element. */
    int top() 
    {
        transfer();

        int ret = m_pIn->front();

        return ret;
    }

    /** Returns whether the stack is empty. */
    bool empty() 
    {
        return m_pIn->empty() && m_pOut->empty();
    }
};

Java:

class MyStack
    {
        private Queue<Integer> m_queueIn=new LinkedList<>();
        private Queue<Integer> m_queueOut=new LinkedList<>();

        void swap()
        {
             Queue<Integer> temp=new LinkedList<>();
             temp=m_queueIn;
             m_queueIn=m_queueOut;
             m_queueOut=temp;
        }
        
        void transfer()
        {
            while(m_queueIn.size()>1)
            {
                m_queueOut.add(m_queueIn.poll());
            }
        }

        public MyStack() {

        }

        /** Push element x onto stack. */
        public void push(int x) 
        {
               m_queueIn.add(x);
        }

        /** Removes the element on top of the stack and returns that element. */
        public int pop() 
        {
               transfer();
               int ret=m_queueIn.poll();
               swap();
               
               return ret;
        }

        /** Get the top element. */
        public int top() 
        {
               transfer();
               
               return m_queueIn.peek();
        }

        /** Returns whether the stack is empty. */
        public boolean empty() 
        {
               return m_queueIn.isEmpty() && m_queueOut.isEmpty();
        }
    }

 Python:

from collections import deque

class MyStack:

    def __init__(self):
        self.data=deque()
        self.help=deque()
       

    def push(self, x: int) -> None:
        self.data.append(x)
        

    def pop(self) -> int:
        while len(self.data)>1:
            self.help.append(self.data.popleft())
        result=self.data.popleft()
        self.help,self.data=self.data,self.help
        return result
        

    def top(self) -> int:
        while len(self.data)>1:
            self.help.append(self.data.popleft())
        result=self.data.popleft()
        self.help.append(result)
        self.help,self.data=self.data,self.help
        return result
       

    def empty(self) -> bool:
        
        return not self.data and not self.help

 

posted @ 2020-04-27 21:02  repinkply  阅读(152)  评论(0)    收藏  举报