Leetcode 225. Implement Stack using Queues

Posted randyniu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 225. Implement Stack using Queues相关的知识,希望对你有一定的参考价值。

class MyStack {
public:
    /** Initialize your data structure here. */
    MyStack() {
        
    }
    /** Push element x onto stack. */
    void push(int x) {
        std::queue<int> aux;
        aux.push(x);
        while(!_data.empty())
        {
            aux.push(_data.front());
            _data.pop();
        }
        swap(aux,_data);
    }
    
    /** Removes the element on top of the stack and returns that element. */
    int pop() {
        int x = _data.front();
        _data.pop();
        return x;
    }
    
    /** Get the top element. */
    int top() {
        return _data.front();
    }
    
    /** Returns whether the stack is empty. */
    bool empty() {
        return _data.empty();
    }
private:
    std::queue<int> _data;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * MyStack obj = new MyStack();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.top();
 * bool param_4 = obj.empty();
 */

 

以上是关于Leetcode 225. Implement Stack using Queues的主要内容,如果未能解决你的问题,请参考以下文章

leetcode?python 225. Implement Stack using Queues

LeetCode 225. Implement Stack using Queues

LeetCode 225 Implement Stack using Queues

[leetcode] 225. Implement Stack using Queues

LeetCode OJ 225Implement Stack using Queues

Leetcode 225 Implement Stack using Queues STL