// Textbook fragment 08.08 /** Returns but does not remove an entry with minimum key. */ public Entry min () throws EmptyPriorityQueueException { if (entries.isEmpty()) throw new EmptyPriorityQueueException("priority queue is empty"); else return entries.first().element(); } /** Inserts a key-value pair and return the entry created. */ public Entry insert (K k, V v) throws InvalidKeyException { checkKey(k); // auxiliary key-checking method (could throw exception) Entry entry = new MyEntry(k, v); insertEntry(entry); // auxiliary insertion method return entry; } /** Auxiliary method used for insertion. */ protected void insertEntry(Entry e) { if (entries.isEmpty()) { entries.addFirst(e); // insert into empty list actionPos = entries.first(); // insertion position } else if (c.compare(e.getKey(), entries.last().element().getKey()) > 0) { entries.addLast(e); // insert at the end of the list actionPos = entries.last(); // insertion position } else { Position> curr = entries.first(); while (c.compare(e.getKey(), curr.element().getKey())> 0) { curr = entries.next(curr); // advance toward insertion position } entries.addBefore(curr, e); actionPos = entries.prev(curr); // insertion position } } /** Removes and returns an entry with minimum key. */ public Entry removeMin() throws EmptyPriorityQueueException { if (entries.isEmpty()) throw new EmptyPriorityQueueException("priority queue is empty"); else return entries.remove(entries.first()); }