题目:

class CQueue {
public:
    stack<int> st1;
    stack<int> st2;
    CQueue() {

    }
    
    void appendTail(int value) {
        st1.push(value);
    }
    
    int deleteHead() {
        if(st1.empty()&&st2.empty())return -1;
        if(!st2.empty()){                      //要先从st2中弹出队列头部元素
            int top=st2.top();
            st2.pop();
            return top;
        }
        while(!st1.empty()){                   //st2为空的时候再把st1中元素全部导入st2中,此后再弹出队列头部元素
            st2.push(st1.top());
            st1.pop();
        }
        int top=st2.top();
        st2.pop();
        return top;
    }
};
posted on 2023-07-27 21:03  孜孜不倦fly  阅读(8)  评论(0)    收藏  举报