简书 賈小強
转载请注明原创出处,谢谢!
package com.lab1.test2;
public class OrderedArrayPQ<Key extends Comparable<Key>> {
private Key[] a = (Key[]) new Comparable[5];
private int n;
private boolean isEmpty() {
return n == 0;
}
private int size() {
return n;
}
private Key delMax() {
return a[--n];
}
private void exch(int i, int j) {
Key temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private boolean less(Key v, Key w) {
return v.compareTo(w) < 0;
}
private void put(Key key) {
int i = n - 1;
for (; i > 0 && less(key, a[i]); i--) {
a[i+1] = a[i];
}
a[i + 1] = key;
n++;
}
public static void main(String[] args) {
OrderedArrayPQ<String> pq = new OrderedArrayPQ<>();
pq.put("bill");
pq.put("jack");
pq.put("lucy");
System.out.println(pq.delMax());
System.out.println(pq.delMax());
System.out.println(pq.delMax());
System.out.println(pq.size());
System.out.println(pq.isEmpty());
}
}
输出
lucy
jack
bill
0
true
Happy learning !!
网友评论