[LeetCode]95. 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.

Notes:

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

 

Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.

 

Subscribe to see which companies asked this question

 
解法1:和题目Implement Queue using Stacks用栈实现队列相似,本题反过来用队列实现栈。方法相同,使用两个队列。需要注意的一点是,在top()操作中,不论是对哪一个队列操作,在取得队尾元素后都还需要将这个队尾元素移到另一个队列中,以保持顺序一致。
class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        while (!q2.empty()) {
            q1.push(q2.front());
            q2.pop();
        }
        q1.push(x);
    }
    // Removes the element on top of the stack.
    void pop() {
        if (!q1.empty()) {
            while (q1.size() > 1) {
                q2.push(q1.front());
                q1.pop();
            }
            q1.pop();
        }
        else {
            while (q2.size() > 1) {
                q1.push(q2.front());
                q2.pop();
            }
            q2.pop();
        }
    }
    // Get the top element.
    int top() {
        if (!q1.empty()) {
            while (q1.size() > 1) {
                q2.push(q1.front());
                q1.pop();
            }
            int ret = q1.front();
            q2.push(ret); // 注意必须将这个top也移到另一个队列
            q1.pop();
            return ret;
        }
        else {
            while (q2.size() > 1) {
                q1.push(q2.front());
                q2.pop();
            }
            int ret = q2.front();
            q1.push(ret); // 注意必须将这个top也移到另一个队列
            q2.pop();
            return ret;
        }
    }
    // Return whether the stack is empty.
    bool empty() {
        return q1.empty() && q2.empty();
    }
private:
    std::queue<int> q1, q2;
};

 

解法2:同样使用一个队列q就可以搞定。在push()操作的时候,只要队列q中有元素,我们先将这些以前的元素搬到另一个队列tmp中暂存起来,然后再push当前元素到队列q中,最后将缓存队列tmp中的所有元素又搬回到q中来。这样最先进入队列的元素恰好要在最后才能取出来的。

class Stack {
public:
    // Push element x onto stack.
    void push(int x) {
        std::queue<int> tmp;
        while (!q.empty()) {
            tmp.push(q.front());
            q.pop();
        }
        q.push(x);
        while (!tmp.empty()) {
            q.push(tmp.front());
            tmp.pop();
        }
    }
    // Removes the element on top of the stack.
    void pop() {
        q.pop();
    }
    // Get the top element.
    int top() {
        return q.front();
    }
    // Return whether the stack is empty.
    bool empty() {
        return q.empty();
    }
private:
    std::queue<int> q;
};

 

posted @ 2015-11-19 11:26  AprilCheny  阅读(171)  评论(0编辑  收藏  举报