码迷,mamicode.com
首页 > 其他好文 > 详细

队列实现栈

时间:2015-08-04 08:07:53      阅读:113      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!