225 Implement Stack using Queues

来源:互联网 发布:java字符串加减乘除 编辑:程序博客网 时间:2024/06/07 01:25
class Stack {public:    // Push element x onto stack.    void push(int x) {        q1.push(x);    }    // Removes the element on top of the stack.    void pop() {        while(q1.size()!=1)        {            q2.push(q1.front());            q1.pop();        }        q1.pop();        q1 = q2;        while(!q2.empty())            q2.pop();    }    // Get the top element.    int top() {        return q1.back();    }    // Return whether the stack is empty.    bool empty() {        return q1.empty();    }private:    queue<int> q1;    queue<int> q2;};

0 0