class PriorityQueue { private int count; // the number of items in the priority queue private int capacity; // the number of available array positions private int capacityIncrement; // the amount to increase the capacity // during array expansion private ComparisonKey[] itemArray; // the array that holds PQ items /*-----------------*/ /** construct an initially empty PriorityQueue */ public PriorityQueue() { // we need to define a no-arg constructor. count = 0; // the empty priority queue has no items capacity = 10; // but there is capacity for ten items capacityIncrement = 5; // and the capacity will expand itemArray = new ComparisonKey[capacity]; // in increments of five } // when necessary /*-----------------*/ /** the size() method returns the count of the number of items */ public int size() { return count; } /*-----------------*/ /** the insert() method inserts a new item into a priority queue */ public void insert(ComparisonKey newItem) { // if the itemArray does not have enough capacity, // expand the itemArray by the capacity increment if (count == capacity) { capacity += capacityIncrement; ComparisonKey[] tempArray = new ComparisonKey[capacity]; for (int i = 0; i < count; i++) { tempArray[i] = itemArray[i]; } itemArray = tempArray; } // insert the newItem at the end of the current sequence of items // and increase the priority queue's count by one itemArray[count++] = newItem; } /*-----------------*/ /** the remove() method removes the highest priority item */ public ComparisonKey remove() { if (count == 0) { // return null if the priority return null; // queue is empty. } else { // otherwise, find the highest priority int maxPosition = 0; // item's position ComparisonKey maxItem = itemArray[0]; for (int i = 1; i < count; i++) { if ( itemArray[i].compareTo(maxItem) > 0 ) { maxPosition = i; maxItem = itemArray[i]; } // then move the last item into } // the hole created by itemArray[maxPosition] = itemArray[--count]; // removing the return maxItem; // highest priority item } // and, return the highest priority item }