传送门

一个栈作为数据存储空间,另建一个辅助栈。每个入队的新元素就插入在存储栈的栈尾。每次删除就将存储栈里的元素依次弹出并压入辅助栈,操作结束后返回辅助栈栈顶元素作为答案,然后再依次将元素弹回存储栈。

class CQueue {
public:
    stack<int> s1;
    stack<int> s2;
    CQueue() {
        while (!s1.empty()) 
            s1.pop();
        while (!s2.empty())
            s2.pop();
    }
    
    void appendTail(int value) {
        s1.push(value);
    }
    
    int deleteHead() {
        if(s1.empty()) return -1;
        
        while(!s1.empty())
        {
            s2.push(s1.top());
            s1.pop();
        }
        int tmp = s2.top();
        s2.pop();

        while(!s2.empty())
        {
            s1.push(s2.top());
            s2.pop();
        }

        return tmp;
    }
};

/**
 * Your CQueue object will be instantiated and called as such:
 * CQueue* obj = new CQueue();
 * obj->appendTail(value);
 * int param_2 = obj->deleteHead();
 */