/** * Initialize your data structure here. Set the size of the queue to be k. */ publicMyCircularQueue(int k) { arr = newint[k]; size = 0; start = 0; end = -1; }
/** * Insert an element into the circular queue. Return true if the operation is * successful. */ publicbooleanenQueue(int value) { if (size == arr.length) { returnfalse; } size++; end = nextIndex(end); arr[end] = value;
returntrue; }
/** * Delete an element from the circular queue. Return true if the operation is * successful. */ publicbooleandeQueue() { if (size == 0) { returnfalse; } System.out.println("deQueue: " + arr[start]); size--; start = nextIndex(start);
returntrue; }
/** * Get the next index. */ publicintnextIndex(int index) { return index == arr.length - 1 ? 0 : index + 1; }
/** * Get the front item from the queue. */ publicintFront() { if (size == 0) { return -1; } return arr[start]; }
/** * Get the last item from the queue. */ publicintRear() { if (size == 0) { return -1; } return arr[end]; }
/** * Checks whether the circular queue is empty or not. */ publicbooleanisEmpty() { return size == 0; }
/** * Checks whether the circular queue is full or not. */ publicbooleanisFull() { return size == arr.length; }