import java.util.ArrayList; /** * A "minimum" priority queue. This always returns the smallest item remaining in the queue * where smallest is given by the comparable key value. The entries in this priority queue are * kept in no particular order. Entries will be returned from the priority queue according to the * ordering specified in the initializer. * * @param the Key element. This must implement Comparable. * @param The Value element. This could be anything. * * For methods that exactly follow the interface, see the documentation in the interface. * * @author gtowell * Created APRIL 6, 2020 */ public class PriorityQueue, V> implements PriorityQInterface { /** enum specifying ordering styles. * ASCENDING and MIN give element with smallest key first * DESCENDING and MAX give element with largest key first */ enum Ordering { ASCENDING, DESCENDING, MIN, MAX} /** ABOUT enum * Use enum when you need a predefined list of values which do represent some kind of numeric or * textual data, you should use an enum. For instance, in a chess game you could represent the * different types of pieces as an enum. Days of the week are also a good use of enum. * * You should always use enums when a variable (especially a method parameter) can only take * one out of a small set of possible values. */ /** * An inner class to keep key/value pair together. This togetherness is all that the class does. */ protected class Entry { /** Hold the key */ final K theK; /** Hold the value*/ final V theV; /** * Create an Entry instance * @param kk the key * @param vv the value */ public Entry(K kk, V vv) { theK = kk; theV = vv; } } /** The thing actually holding the data in the priority queue. Using an ArrayList * to get unbounded length. */ final private ArrayList pqStore; /** The order in which elements are removed from the priority queue */ final private Ordering order; /** * Initialize the priority queue */ public PriorityQueue() { this(Ordering.MIN); } public PriorityQueue(Ordering order) { this.order=order; pqStore = new ArrayList<>(); } @Override public int size() { return pqStore.size(); } @Override public boolean isEmpty() { return pqStore.isEmpty(); } @Override public boolean offer(K newK, V newV) { Entry entry = new Entry(newK, newV); pqStore.add(entry); // Note that this always succeeds, so always return true. return true; } /** * find and return the next item from the priority queue * @return the next item */ private Entry getNext() { if (isEmpty()) return null; // if there are entries, then go through them all to find the next one. // must do this because entries are in no particular order. Entry cmin = pqStore.get(0); // see with the item in position 0 // the start doing comparisons with item in position 1. for (int i=1; i 0) cmin=curr; break; } } return cmin; } @Override public V poll() { Entry entry = getNext(); if (entry==null) return null; pqStore.remove(entry); return entry.theV; } @Override public V peek() { Entry entry = getNext(); if (entry==null) return null; return entry.theV; } public static void main(String[] args) { PriorityQueue pq = new PriorityQueue<>(Ordering.MIN); pq.offer(1,"Jane"); pq.offer(10,"WET"); pq.offer(5, "WAS"); System.out.println(pq.poll()); System.out.println(pq.poll()); System.out.println(pq.poll()); System.out.println(); pq = new PriorityQueue<>(Ordering.MAX); pq.offer(1,"Jane"); pq.offer(10,"WET"); pq.offer(5, "WAS"); System.out.println(pq.poll()); System.out.println(pq.poll()); System.out.println(pq.poll()); } }