Priority Queue
package dataStructures;
public interface Queue {
public void enqueue(Object value);
public Object dequeue() throws EmptyQueueException;
public void clear();
public int size();
public boolean isEmpty();
}
package sorting;
public interface Comparator {
public int compare(Object left, Object right);
}
package dataStructures;
import sorting.Comparator;
public class SortedListPriorityQueue implements Queue {
private final List _list;
private final Comparator _comparator;
public SortedListPriorityQueue(Comparator comparator) {
assert comparator != null : "comparator cannot be null";
_comparator = comparator;_list = new LinkedList();
}
public void enqueue(Object value) {
int pos = _list.size();
while (pos > 0 && _comparator.compare(_list.get(pos - 1), value) > 0) {
--pos;
}
_list.insert(pos, value);
}
public Object dequeue() throws EmptyQueueException {
if (isEmpty()) {
throw new EmptyQueueException();
}
return _list.delete(_list.size() - 1);
}
public void clear() {
_list.clear();
}
public int size() {
return _list.size();
}
public boolean isEmpty() {
return _list.isEmpty();
}
}