出队顺序和入队顺序无关。优先级相关,本身还是队列
应用:任务管理器中选择优先级高的任务执行
游戏中塔防类游戏优先攻击(距离、威胁、先后)
import com.oupeng.p3队列.Queue;
public class PriorityQueue<E extends Comparable<E>> implements Queue<E> {
private MaxHeap<E> heap;
public PriorityQueue() {
heap = new MaxHeap<E>();
}
@Override
public int getSize() {
// TODO Auto-generated method stub
return heap.size();
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return heap.isEmpty();
}
@Override
public void clear() {
heap.clear();
}
@Override
public void enqueue(E e) {
heap.add(e);
}
@Override
public E dequeue() {
return heap.extractMax();
}
@Override
public E getFront() {
return heap.findMax();
}
@Override
public E getRear() {
return heap.findMin();
}
}