leetcode 232.Implement Queue using Stacks

class MyQueue {

private:

    stack<int> queue;

public:

    /** Initialize your data structure here. */

    MyQueue() {

        

    }

    /** Push element x to the back of queue. */

    void push(int x) {

        stack<int> a;

        while(!queue.empty())

        {

            a.push(queue.top());

            queue.pop();

        }

        a.push(x);

        while(!a.empty())

        {

            queue.push(a.top());

            a.pop();

        }

    }

    

    /** Removes the element from in front of queue and returns that element. */

    int pop() {

        int x=queue.top();

        queue.pop();

        return x;

    }

    

    /** Get the front element. */

    int peek() {

        return queue.top();

    }

    

    /** Returns whether the queue is empty. */

    bool empty() {

        if(queue.empty())

            return true;

        return false;

    }

};


你可能感兴趣的:(leetcode 232.Implement Queue using Stacks)