LeetCode:622. 设计循环队列

    技术2022-07-13  64

    class MyCircularQueue { public: list<int>obj; int size=0; int capacity; /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) { capacity=k; } /** Insert an element into the circular queue. Return true if the operation is successful. */ bool enQueue(int value) { if(size==capacity) { return false; } size++; obj.push_back(value); return true; } /** Delete an element from the circular queue. Return true if the operation is successful. */ bool deQueue() { if(obj.size()==0) { return false; } size--; obj.erase(obj.begin()); return true; } /** Get the front item from the queue. */ int Front() { if(obj.size()==0) { return -1; } return obj.front(); } /** Get the last item from the queue. */ int Rear() { if(obj.size()==0) { return -1; } return obj.back(); } /** Checks whether the circular queue is empty or not. */ bool isEmpty() { return obj.size()==0; } /** Checks whether the circular queue is full or not. */ bool isFull() { return obj.size()==capacity; } }; /** * Your MyCircularQueue object will be instantiated and called as such: * MyCircularQueue* obj = new MyCircularQueue(k); * bool param_1 = obj->enQueue(value); * bool param_2 = obj->deQueue(); * int param_3 = obj->Front(); * int param_4 = obj->Rear(); * bool param_5 = obj->isEmpty(); * bool param_6 = obj->isFull(); */
    Processed: 0.011, SQL: 9