//循环队列 约定:head = -1且tail=-1时为空 class MyCircularQueue { private: int* elements; int head; int tail; int size; public: /* initialize your data structure here, set the size of the queue to be k*/ MyCircularQueue(int k) { elements = new int[k]; size = k; head = tail = -1; } ~MyCircularQueue() { delete[] elements; } /*insert an element into the circular queue.Return true if the operation is successful*/ bool enQueue(int value) { if (isFull()) return false; if (isEmpty()) head++; tail = (tail + 1) % size; elements[tail] = value; } bool deQueue() { if (isEmpty()) return false; //约定head=-1&&tail=-1为空 if (head == tail) head = tail = -1; else head = (head + 1) % size; return true; } /*get the front the item from the queue*/ int Front() { if (isEmpty()) return -1; return elements[head]; } /*get the last item from the queue*/ int Rear() { if (isEmpty()) return -1; return elements[tail]; } /*check whether the circular queue is empty or not*/ //约定head=-1且tail=-1时为空 bool isEmpty() { return head == -1 && tail == -1; } /*check whether the circular queue is full or not*/ bool isFull() { return (tail + 1) % size == head; } }; /*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(value) int param_3 = obj->Front(); int param_4 = obj->Rear(); bool param_5 = obj->isEmpty(); bool param_6 = obj->isFull(); */