// implement a priority queue using a linked list // first item on list is the highest priority item public class PQAsList implements SeqStructure{ private SLL list; private int size; public PQAsList() { list = new SLL(); } // enqueue: put o at end of list: public void put(Object o) { list.add(o); size++; } // dequeue: remove 1st occurrence of largest element in list: public Object get() { if (isEmpty()) { System.out.print("Empty!"); return null; } // search list, starting from head: ListNode n = list.getHead(); Object max = n.getItem(); // assume head's item is biggest ListNode next = n.getNext(); // find and update max: while (next != null) { Object current = next.getItem(); int comp = ((Comparable)current).compareTo(max); if (comp > 0) max = current; next = next.getNext(); } list.remove(max); size--; return max; } // empty PQ means empty list: public boolean isEmpty() { return list.isEmpty(); } // return number of elements: public int size() { return size; } // Stringify: public String toString() { return "PQ: ["+list+"]"; } }