package rebelsky.linear; import java.util.Vector; /** * An implementation of priority queues using Vectors in which the * elements are stored in no particular order, so the smallest * is identified during a call to get(). * * @author CSC152 2006S * @version 0.0000000001 of March 2006 */ public class PQUV> implements PriorityQueue { // +------------------+------------------------------------- // | Design Decisions | // +------------------+ /* We need a way to compare elements in the Vector. We will use the internal compareTo method. */ // +--------+----------------------------------------------- // | Fields | // +--------+ Vector values; // +--------------+----------------------------------------- // | Constructors | // +--------------+ public PQUV() { this.values = new Vector(); } // PQUV() // +----------------+--------------------------------------- // | Public Methods | // +----------------+ public T get() { // Guess that the first element of the vector is the smallest // We'll keep track of position of smallest, rather than the value int alpha = 0; // Compare to each remaining element, updating the guess as appropriate for (int i = 1; i < this.values.size(); i++) { if (this.values.get(i).compareTo(this.values.get(alpha)) < 0) { alpha = i; } // if } // // Remember the component T val = this.values.get(alpha); // Remove it (the following line is inefficient; we should // really take the last element and put it in space alpha). // Note: You may see this on an exam. this.values.remove(alpha); // We're done return val; } // get() public boolean isEmpty() { return this.values.isEmpty(); } // isEmpty() public void put(T val) { // Put val in the first empty spot this.values.add(val); } // put(T) } // class PQUV