剑指 Offer 09. 用两个栈实现队列

class CQueue {
private:
    stack<int> a, b;
public:
    CQueue() {
        // stack<int> a, b;
    }
    
    void appendTail(int value) {
        a.push(value);
    }
    
    int deleteHead() {
        if( a.empty() )return -1;
        while( !a.empty() ){
            int c = a.top();
            a.pop();
            b.push(c);
        }
        int res = b.top();
        b.pop();
        while( !b.empty() ){
            int c = b.top();
            b.pop();
            a.push(c);
        }
        return res;
    }
};

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

 

上一篇:[算法题打卡-1] 剑指 Offer 09. 用两个栈实现队列


下一篇:剑指offer09 : 用两个栈实现队列