其他分享
首页 > 其他分享> > 用两个栈模拟队列

用两个栈模拟队列

作者:互联网

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(!s2.empty()){
            
        }
        else{
            if(s1.empty())
                return -1;
            while(!s1.empty()){
            s2.push(s1.top());
            s1.pop();
            }
        }
        int re=s2.top();
        s2.pop();
        return re;
        
    }
};

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

标签:两个,队列,s2,s1,pop,int,empty,CQueue,模拟
来源: https://blog.csdn.net/qq_44082048/article/details/121050172