package dataStructures;
import java.util.NoSuchElementException;
public class ArrayPQ implements PriorityQueue {
private ArrayList list;
public ArrayPQ(int cap) {
list = new ArrayList(cap);
}
public boolean isEmpty() { return list.isEmpty(); }
public int size() { return list.size(); }
public void enqueue(Object e) { list.add(e); }
public Object peek() { return list.get(maxIndex()); }
public Object dequeue() {
int max = maxIndex();
Object result = list.get(max);
list.remove(max);
return result;
}
private int maxIndex() {
if (isEmpty()) throw new NoSuchElementException();
int max = 0;
for (int i = 1; i < list.size(); i++) {
Comparable d = (Comparable) list.get(i);
if (d.compareTo(list.get(max)) < 0) max = i;
}
return max;
}
}