classMyCircularQueue { public: /** Initialize your data structure here. Set the size of the queue to be k. */ MyCircularQueue(int k) : k(k) { q.resize(k); }
/** Insert an element into the circular queue. Return true if the operation is successful. */ boolenQueue(int value){ if (isFull()) returnfalse; q[e] = value; e = (e + 1) % k; is_empty = false; is_full = b == e; returntrue; }
/** Delete an element from the circular queue. Return true if the operation is successful. */ booldeQueue(){ if (isEmpty()) returnfalse; b = (b + 1) % k; is_full = false; is_empty = b == e; returntrue; }
/** Get the front item from the queue. */ intFront(){ return isEmpty() ? -1 : q[b]; }
/** Get the last item from the queue. */ intRear(){ return isEmpty() ? -1 : q[(e + k - 1) % k]; }
/** Checks whether the circular queue is empty or not. */ boolisEmpty(){ return is_empty; }
/** Checks whether the circular queue is full or not. */ boolisFull(){ return is_full; }
int b = 0, e = 0, k; vector<int> q; bool is_full = false, is_empty = true; };
/** * 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(); */