In a priority queue, items are ordered by key value, so that the item with the lowest
key (or in some implementations the highest key) is always at the front.
Items are inserted in the proper position to maintain the order.
class PriorityQueue {
private int maxSize;
private double[] queArray;
private int nItems;
public PriorityQueue(int s) {
maxSize = s;
queArray = new double[maxSize];
nItems = 0;
}
public double remove() { return queArray[--nItems];
}
public double peekMin() { return queArray[nItems - 1];
}
public boolean isEmpty() { return (nItems == 0);
}
public boolean isFull() { return (nItems == maxSize);
}
}
public class MainClass { public static void main(String[] args) {
PriorityQueue thePQ = new PriorityQueue(5);
thePQ.insert(30);
thePQ.insert(50);
thePQ.insert(10);
thePQ.insert(40);
thePQ.insert(20);