Question: Create the ADT priority queue by implementing attached PriorityQueueInterface interface . Use a chain of linked nodes for your implementation and call the class LinkedPriorityQueue
Create the ADT priority queue by implementing attached PriorityQueueInterface interface .
Use a chain of linked nodes for your implementation and call the class LinkedPriorityQueue
Your implementation must use generics and be able to work with any object Type that is Comparable.
You are free to use circular or regular chain for your implementation but not array based implementation.
Use attached Driver class to test your implementation.
The Driver class uses attached Entry class (which is Comparable) as data type.
The driver creates different instances of Entry with different values and store them in your priority queue implementation.
and the retrieve from the queue. your implementation priorities items based on their value.
You are not supposed to modify any of the attached file. But once you put your implementation of LinkedPriorityQueue in the same folder as the other 3 files, the Driver class should compile and run.
/** An interface for the ADT priority queue. */ public interface PriorityQueueInterface> { /** Adds a new entry to this priority queue. @param newEntry An object to be added. */ public void add(T newEntry); /** Removes and returns the entry having the highest priority. @return Either the object having the highest priority or, if the priority queue is empty before the operation, null. */ public T remove(); /** Retrieves the entry having the highest priority. @return Either the object having the highest priority or, if the priority queue is empty, null. */ public T peek(); /** Detects whether this priority queue is empty. @return True if the priority queue is empty, or false otherwise. */ public boolean isEmpty(); /** Gets the size of this priority queue. @return The number of entries currently in the priority queue. */ public int getSize(); /** Removes all entries from this priority queue. */ public void clear(); } // end PriorityQueueInterface
/** A driver that demonstrates the class LinkedPriorityQueue. */ public class Driver { public static void main(String[] args) { testPriorityQueueOperations(); System.out.println(" Done."); } // end main public static void testPriorityQueueOperations() { // Create 9 Things with priorities 1 - 9 Entry one = new Entry<>("one", 1); Entry two = new Entry<>("two", 2); Entry three = new Entry<>("three", 3); Entry four = new Entry<>("four", 4); Entry five = new Entry<>("five", 5); Entry six = new Entry<>("six", 6); Entry seven = new Entry<>("seven", 7); Entry eight = new Entry<>("eight", 8); Entry nine = new Entry<>("nine", 9); // Add them to a priority queue System.out.println("Create a priority queue: "); PriorityQueueInterface> myPQ = new LinkedPriorityQueue<>(); System.out.println(" isEmpty() returns " + myPQ.isEmpty() + " "); System.out.println("Add 9 entries in no particular order"); myPQ.add(nine); myPQ.add(eight); myPQ.add(seven); myPQ.add(one); myPQ.add(three); myPQ.add(two); myPQ.add(four); myPQ.add(six); myPQ.add(five); System.out.print(" The priority queue should not be empty: "); System.out.println(" isEmpty returns " + myPQ.isEmpty() + " "); System.out.println(" The priority queue should have 9 entries. " + "getSize returns " + myPQ.getSize()); System.out.println(" Testing peek and remove: "); System.out.println("Entries should appear in priority order. "); while (!myPQ.isEmpty()) { Entry front = myPQ.peek(); System.out.println(front + " is at the front of the priority queue."); front = myPQ.remove(); System.out.println(front + " is removed from the front of the priority queue. "); } // end while System.out.print(" The priority queue should be empty: "); System.out.println("isEmpty() returns " + myPQ.isEmpty() + " "); System.out.println("Add 4 entries:"); myPQ.add(three); myPQ.add(nine); myPQ.add(one); myPQ.add(seven); System.out.print(" The priority queue should not be empty: "); System.out.println(" isEmpty returns " + myPQ.isEmpty() + " "); System.out.println(" The priority queue should have 4 entries. " + "getSize returns " + myPQ.getSize()); System.out.println(" Testing clear: "); myPQ.clear(); System.out.print(" The priority queue should be empty: "); System.out.println("isEmpty returns " + myPQ.isEmpty() + " "); System.out.println(" The priority queue should have no entries. " + "getSize returns " + myPQ.getSize()); } // end testPriorityQueueOperations } // end Driver /* Create a priority queue: isEmpty() returns true Add 9 entries in no particular order The priority queue should not be empty: isEmpty returns false The priority queue should have 9 entries. getSize returns 9 Testing peek and remove: Entries should appear in priority order. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. item/priority is at the front of the priority queue. item/priority is removed from the front of the priority queue. The priority queue should be empty: isEmpty() returns true Add 4 entries: The priority queue should not be empty: isEmpty returns false The priority queue should have 4 entries. getSize returns 4 Testing clear: The priority queue should be empty: isEmpty returns true The priority queue should have no entries. getSize returns 0 Done. */
/** A class that represents an entry for a priority queue. */ public class Entry> implements Comparable > { private E theItem; private P thePriority; public Entry(E item, P priority) { theItem = item; thePriority = priority; } // end constructor public E getItem() { return theItem; } // end getItem public P getPriority() { return thePriority; } // end getPriority public int compareTo(Entry other) { return thePriority.compareTo(other.thePriority); } // end compareTo public String toString() { return "item/priority <" + theItem + ", " + thePriority + ">"; } // end toString } // end Entry
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
