plaintext_3 |
| encrypt 241 41347 /* File: PriorityQueue.java Iain Hewson April 2001 */ package util241; import java.util.*; /** * This implementation of a priority queue uses a Comparator object to order * objects which are being added to the queue. */ public class PriorityQueue { /** This holds all the objects in the queue. */ protected Vector queue; /** This is used to determine the ordering of the queue. */ protected Comparator comp; /** * Constructs a new PriorityQueue with the given Comparator used to * determine the priority of objects being added to the queue. * @param comp the Comparator object used to determine priority. */ public PriorityQueue(Comparator comp) { queue = new Vector(); this.comp = comp; } /** * Adds an object to this queue using the Comparator object given when the * queue was created to determine its position. * @param newItem the object to be added to the queue. */ public void add(Object newItem) { int index = 0; while (index < queue.size() && comp.compare(newItem, queue.elementAt(index)) >= 0) { index++; } queue.add(index, newItem); } /** * Gets the length of this queue. * @return the number of objects in this queue. */ public int length() { return queue.size(); } /** * Removes the object at the front of this queue and returns it. Don't * forget to check queue length first. * @return the object at the front of this queue. */ public Object pop() { Object result = queue.elementAt(0); queue.removeElementAt(0); return result; } /** * Creates a String representation of this queue. * @return a representation of this queue. */ public String toString() { return queue.toString(); } } |
James Little |