剑指offer面试题09. 用两个栈实现队列

class CQueue {
public:
    stack<int> a;
    stack<int> b;
    CQueue() {
        
    }
    
    void appendTail(int value) {
        a.push(value);
    }
    
    int deleteHead() {
        if(a.empty()&&b.empty())return -1;
        else if(!b.empty())
        {
        }else if(!a.empty())
        {
            while(!a.empty())
            {
                b.push(a.top());
                a.pop();
            }
        }
        int val=b.top();
            b.pop();
            return val;
    }
};

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

剑指offer面试题09. 用两个栈实现队列_第1张图片
只要b不空,就输出

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