美文网首页
232. Implement Queue using Stack

232. Implement Queue using Stack

作者: SilentDawn | 来源:发表于2018-08-17 14:44 被阅读0次

    Problem

    Implement the following operations of a queue using stacks.

    • push(x) -- Push element x to the back of queue.
    • pop() -- Removes the element from in front of queue.
    • peek() -- Get the front element.
    • empty() -- Return whether the queue is empty.

    Notes:

    You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
    Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
    You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

    Example

    MyQueue queue = new MyQueue();
    
    queue.push(1);
    queue.push(2);  
    queue.peek();  // returns 1
    queue.pop();   // returns 1
    queue.empty(); // returns false
    

    Code

    static int var = [](){
        std::ios::sync_with_stdio(false);
        cin.tie(NULL);
        return 0;
    }();
    class MyQueue {
    private:
        stack<int> s;
    public:
        /** Initialize your data structure here. */
        MyQueue() {
            
        }
        
        /** Push element x to the back of queue. */
        void push(int x) {
            s.push(x);
        }
        
        /** Removes the element from in front of queue and returns that element. */
        int pop() {
            stack<int> temp;
            int s_size = s.size();
            for(int i=0;i<s_size-1;i++){
                temp.push(s.top());
                s.pop();
            }
            int ret = s.top();
            s.pop();
            int t_size = temp.size();
            for(int i=0;i<t_size;i++){
                s.push(temp.top());
                temp.pop();
            }
            return ret;
        }
        
        /** Get the front element. */
        int peek() {
            stack<int> temp;
            int s_size = s.size();
            for(int i=0;i<s_size-1;i++){
                temp.push(s.top());
                s.pop();
            }
            int ret = s.top();
            int t_size = temp.size();
            for(int i=0;i<t_size;i++){
                s.push(temp.top());
                temp.pop();
            }
            return ret;
        }
        
        /** Returns whether the queue is empty. */
        bool empty() {
            return s.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();
     * bool param_4 = obj.empty();
     */
    

    Result

    232. Implement Queue using Stacks.png

    相关文章

      网友评论

          本文标题:232. Implement Queue using Stack

          本文链接:https://www.haomeiwen.com/subject/tjzzbftx.html