Data structure source code
The implementation class
public class PriorityQueue<E extends Comparable<E>> implements Queue<E> { private MaxHeap<E> maxHeap; public PriorityQueue() { maxHeap = new MaxHeap<>(); } @Override public int getSize() { return maxHeap.size(); } @Override public boolean isEmpty() { return maxHeap.isEmpty(); } @Override public E getFront() { return maxHeap.findMax(); } @Override public void enqueue(E e) { maxHeap.add(e); } @Override public E dequeue() { return maxHeap.extractMax(); }}Copy the code
Data structure disassembly
Maintain fields and inner classes
private MaxHeap<E> maxHeap;
Copy the code
The constructor
public PriorityQueue() {
maxHeap = new MaxHeap<>();
}
Copy the code
increase
@Override
public void enqueue(E e) {
maxHeap.add(e);
}
Copy the code
delete
@Override
public E dequeue() {
return maxHeap.extractMax();
}
Copy the code
change
check
@Override
public int getSize() {
return maxHeap.size();
}
@Override
public boolean isEmpty() {
return maxHeap.isEmpty();
}
@Override
public E getFront() {
return maxHeap.findMax();
}
Copy the code