首页 > 其他 > 详细

队列实现栈

时间:2015-08-04 08:06:52      阅读:242      评论:0      收藏:0      [点我收藏+]
class Stack {
private:
    queue<int> que;
public:
    // Push element x onto stack.
    void push(int x) {
        que.push(x);
    }
    
    // Removes the element on top of the stack.
    void pop() {
        queue<int> popedQue;
        auto len = que.size();
        for (size_t i = 0; i < len - 1; ++i){
            popedQue.push(que.front());
            que.pop();
        }
        que = popedQue;
        // pop que except tail
        // then que = popedQue
    }
    
    // Get the top element.
    int top() {
        return que.back();
    }
    
    // Return whether the stack is empty.
    bool empty() {
        return que.empty();
    }
};

 

队列实现栈

原文:http://www.cnblogs.com/wuOverflow/p/4700902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!