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

leetcode

C++:

class CQueue {
public:
    std::stack headStack;
    std::stack tailStack;

public:
    CQueue() {

    }
    
    void appendTail(int value) {

        this -> tailStack.push(value);
    }
    
    int deleteHead() {

        if ( this -> headStack.empty() ) {

            while ( !( this -> tailStack.empty() ) ) {

                this -> headStack.push( this -> tailStack.top() );
                this -> tailStack.pop();
            }
        }

        if ( !( this -> headStack.empty() ) ) {

            int returnedValue = this -> headStack.top();
            this -> headStack.pop();

            return returnedValue;
        }

        return -1;
    }
};

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

你可能感兴趣的:(leetcode 剑指 Offer 09. 用两个栈实现队列)