两个队列实现一个栈

两个队列实现一个栈:
class Stack {
    private :
    queue q[2];
    int current = 0;
    public :
    void Push(int data) {
        q[current].push(data);
    }
    int Pop() {
        int result = 0;
        int n = (int)q[current].size();
        if (n!= 0) {
            for (int i = 0; i < n -1; i ++) {
                int tmp = q[current].front();
                q[current].pop();
                q[1-current].push(tmp);
            }
            result = q[current].front();
            q[current].pop();
            current = 1 - current;
        }
        return result;
    }
};

你可能感兴趣的:(算法)