Posted on

Description

Submission

class FrontMiddleBackQueue {
    vector<int> q;

    // void displayQueue() {
    //     for(int i = 0; i < q.size(); ++i) {
    //         cout << q[i] << " ";
    //     }
    //     cout << endl;
    // }
public:
    FrontMiddleBackQueue() {
        
    }
    
    void pushFront(int val) {
        q.insert(q.begin(), val);
        // displayQueue();
    }
    
    void pushMiddle(int val) {
        q.insert(q.begin() + q.size() / 2, val);
        // displayQueue();
    }
    
    void pushBack(int val) {
        q.push_back(val);
        // displayQueue();
    }
    
    int popFront() {
        if(q.empty()) return -1;
        int ret = q.front();
        q.erase(q.begin());
        // displayQueue();
        return ret;
    }
    
    int popMiddle() {
        if(q.empty()) return -1;
        int ret = q[(q.size()-1)/2];
        q.erase(q.begin() + (q.size()-1)/2);
        // displayQueue();
        return ret;
    } 
    
    int popBack() {
        if(q.empty()) return -1;
        int ret = q.back();
        q.pop_back();
        // displayQueue();
        return ret;
    }
};

/**
 * Your FrontMiddleBackQueue object will be instantiated and called as such:
 * FrontMiddleBackQueue* obj = new FrontMiddleBackQueue();
 * obj->pushFront(val);
 * obj->pushMiddle(val);
 * obj->pushBack(val);
 * int param_4 = obj->popFront();
 * int param_5 = obj->popMiddle();
 * int param_6 = obj->popBack();
 */

Leave a Reply

Your email address will not be published. Required fields are marked *