package gray.adts.queue; import java.lang.*; public class PriorityQueue extends ListQueue { PriorityQueue() { super(); } ListQueue tempholder = new ListQueue(); // add the newItem with a priority value of "0" to the back of // the priority queue. public void enqueue(Object newItem) { this.enqueue(newItem,0); } // add the newItem with a priority value of "pri" into the priority // queue so that it is located behind all other items with equal or // higher priority but ahead of all items with lower priority. public void enqueue(Object newItem, int pri) { PrioritizedItem pItem = new PrioritizedItem(newItem, pri); PrioritizedItem ptemp= new PrioritizedItem(); while(ptemp.getPriority()<=pri && !super.isEmpty()) { ptemp=(PrioritizedItem)super.peek(); if(ptemp.getPriority()<=pri) tempholder.enqueue(super.dequeue()); } super.enqueue(pItem); while(!tempholder.isEmpty()) super.enqueue(tempholder.dequeue()); } public void PrintQueue() { PrioritizedItem ptemp= new PrioritizedItem(); while(!super.isEmpty()) { ptemp=(PrioritizedItem)super.dequeue(); System.out.print(" [ "+ ptemp.getPrioritizedItem()+"(pri:"+ ptemp.getPriority() + ") ]"); } //System.out.println(super.peek()); } public static void main(String args[]) { PriorityQueue blah = new PriorityQueue(); blah.enqueue(1, 1); //Must start with a low # otherwise bad things will happen blah.enqueue(5, 10); blah.enqueue("zero", 0); blah.enqueue(0, 0); blah.enqueue(5, 10); blah.enqueue(30, 30); blah.enqueue("super 900 pie", 900); blah.PrintQueue(); } }