packageidv.carl.datastructures.queue;/** * @author Carl Lu */publicclassPriorityQueue {privateint[] queue;// Number of elements in this queueprivateint elementCount;publicPriorityQueue(int length) { queue =newint[length]; elementCount =0; }publicvoidinsert(int element) {if (elementCount ==queue.length) {return; } elseif (elementCount ==0) { queue[elementCount] = element; } else {/* * If the queue is not empty, execute sorting before insert the element */int i;for (i = elementCount -1; i >=0; i--) {if (element > queue[i]) { queue[i +1] = queue[i]; } else {break; } }/* * Since we use i-- in the for loop on line 28, * so here we need to add 1 for the index i. */ queue[i +1] = element; } elementCount++; }publicintremove() {if (elementCount ==0) {return0; }// Decrease the elementCount because it already be increased at the end of insert. elementCount--;// Remove the last elementint removed = queue[elementCount];// Assume that 0 means the data was removed queue[elementCount] =0;return removed; }publicintpeek() {return queue[elementCount -1]; }publicbooleanisEmpty() {return elementCount ==0; }publicbooleanisFull() {return elementCount ==queue.length; }publicintgetElementCount() {return elementCount; }}