packageidv.carl.datastructures.queue;/** * @author Carl Lu */publicclassCircularQueue {privateint[] queue;privateint head;privateint tail;// Number of elements in this queueprivateint elementCount;publicCircularQueue(int length) { queue =newint[length]; head =0; tail =-1; elementCount =0; }publicvoidinsert(int element) {// Check the tail index already exceed the max length or notif (tail ==queue.length-1) { tail =-1; } tail++; queue[tail] = element; elementCount++;if (elementCount >queue.length) { elementCount =queue.length; } }publicintremove() {if (elementCount ==0) {return0; }int temp = queue[head]; queue[head] =0;// Check the head index already exceed the max length or notif (head ==queue.length-1) {/* * If the removed node is tail, it means that the next node will be removed must be the * head node since this is a circular queue, so reset head index to 0. */ head =0; } else { head++; } elementCount--;return temp; }publicintpeek() {return queue[head]; }publicbooleanisEmpty() {return elementCount ==0; }publicbooleanisFull() {return elementCount ==queue.length; }publicintgetElementCount() {return elementCount; }}