-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPQUnsorted.java
51 lines (40 loc) · 904 Bytes
/
PQUnsorted.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class PQUnsorted implements PriorityQueue {
private Integer[] data;
private int numElts;
public void add(Integer toAdd) {
if(numElts == data.length) resize();
data[numElts] = toAdd;
numElts++;
}
public Integer remove() {
if(numElts == 0) return null;
Integer highestPrioLoc = 0;
for(int i = 1; i < numElts; i++) {
if(data[i] > data[highestPrioLoc]) {
highestPrioLoc = i;
}
}
Integer toReturn = data[highestPrioLoc];
numElts--;
data[highestPrioLoc] = data[numElts];
data[numElts] = null;
return toReturn;
}
public int size() {
return numElts;
}
public boolean isEmpty() {
return numElts == 0;
}
private void resize() {
Integer[] temp = new Integer[numElts * 2];
for(int i = 0; i < numElts; i++) {
temp[i] = data[i];
}
data = temp;
}
public PQUnsorted() {
data = new Integer[2];
numElts = 0;
}
}