其他分享
首页 > 其他分享> > 用两个栈实现队列

用两个栈实现队列

作者:互联网

在这里插入图片描述

class CQueue {
public:
    stack<int>InStack;
    stack<int>OutStack;
    CQueue() {

    }
    
    void appendTail(int value) {
        InStack.push(value);
    }
    
    int deleteHead() {
        if(InStack.empty()) 
                return -1;
        if(OutStack.empty())
        {
            while(!InStack.empty())
            {
                OutStack.push(InStack.top());
                InStack.pop();
            }
        }
        int result=OutStack.top();
        OutStack.pop();
        
        while(!OutStack.empty())
        {
            InStack.push(OutStack.top());
            OutStack.pop();
        }
        return result;
    }
};

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

在这里插入图片描述

标签:两个,队列,CQueue,OutStack,pop,实现,int,InStack,empty
来源: https://blog.csdn.net/qq_41363459/article/details/113802454