leetcode 225. Implement Stack using Queues

来源:互联网 发布:中兴刷机软件 编辑:程序博客网 时间:2024/06/09 18:49
class Stack {public:    // Push element x onto stack.    queue<int> stack;    void push(int x) {        stack.push(x);    }    // Removes the element on top of the stack.    void pop() {        queue<int>x;        while(stack.size() > 1){            x.push(stack.front());            stack.pop();        }        if(stack.size()>0)stack.pop();        while(x.size()>0){            stack.push(x.front());            x.pop();        }    }    // Get the top element.    int top() {        return stack.back();         queue<int>x;        while(stack.size() > 1){            x.push(stack.front());            stack.pop();        }        int ans = 0;        if(stack.size()>0){            ans = stack.front();            x.push(ans);            stack.pop();        }        while(x.size()>0){            stack.push(x.front());            x.pop();        }        return ans;    }    // Return whether the stack is empty.    bool empty() {        return stack.size() == 0;    }};

0 0
原创粉丝点击