LeetCode:641. 設計循環雙端隊列

class MyCircularDeque {
public:
    list<int>obj;
    int size=0;
    int capacity;
    /** Initialize your data structure here. Set the size of the deque to be k. */
    MyCircularDeque(int k) {
        this->capacity=k;
    }
    
    /** Adds an item at the front of Deque. Return true if the operation is successful. */
    bool insertFront(int value) {
        if(this->capacity==obj.size())
        {
            return false;
        }
        obj.push_front(value);
        size++;
        return true;
    }
    
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    bool insertLast(int value) {
         if(this->capacity==obj.size())
        {
            return false;
        }
        obj.push_back(value);
        size++;
        return true;
    }
    
    /** Deletes an item from the front of Deque. Return true if the operation is successful. */
    bool deleteFront() {
        if(obj.size()>0)
        {
            obj.pop_front();
            size--;
            return true;
        }
        return false;

    }
    
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    bool deleteLast() {
        if(obj.size()>0)
        {
            obj.pop_back();
            size--;
            return true;
        }
        return false;
    }
    
    /** Get the front item from the deque. */
    int getFront() {
        if(obj.size()==0)
        {
            return -1;
        }
        return obj.front();
    }
    
    /** Get the last item from the deque. */
    int getRear() {
        if(obj.size()==0)
        {
            return -1;
        }
        return obj.back();
    }
    
    /** Checks whether the circular deque is empty or not. */
    bool isEmpty() {
        return obj.size()==0;
    }
    
    /** Checks whether the circular deque is full or not. */
    bool isFull() {
        return obj.size()==capacity;
    }
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque* obj = new MyCircularDeque(k);
 * bool param_1 = obj->insertFront(value);
 * bool param_2 = obj->insertLast(value);
 * bool param_3 = obj->deleteFront();
 * bool param_4 = obj->deleteLast();
 * int param_5 = obj->getFront();
 * int param_6 = obj->getRear();
 * bool param_7 = obj->isEmpty();
 * bool param_8 = obj->isFull();
 */
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章