leetcode 日经贴,Cpp code -Implement Stack using Queues

来源:互联网 发布:udid定制后台源码 编辑:程序博客网 时间:2024/06/07 19:13

Implement Stack using Queues

class Stack {    queue<int> q;    int topval;public:    // Push element x onto stack.    void push(int x) {        q.push(x);        topval = x;    }    // Removes the element on top of the stack.    void pop() {        int sz = q.size();        for (int i = 1; i < sz; ++i) {            topval = q.front();            q.pop();            q.push(topval);        }        q.pop();    }    // Get the top element.    int top() {        return topval;    }    // Return whether the stack is empty.    bool empty() {        return q.empty();    }};


0 0