-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPriorityQueue.java
More file actions
52 lines (44 loc) · 1.24 KB
/
MyPriorityQueue.java
File metadata and controls
52 lines (44 loc) · 1.24 KB
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
52
class PQueue{
private int capacity;
private int numberOfElements;
private int [] arr;
public PQueue(int capacity){
numberOfElements =0;
this.capacity = capacity;
arr = new int[capacity];
for(int i=0; i<capacity;i++) {
arr[i] = Integer.MAX_VALUE;
}
}
public void insertSorted(int value) {
numberOfElements++;
// This is the check we were missing during the lecture!!!
// For the first element, since the array is already initialized at 0
if (numberOfElements == 0) {
arr[numberOfElements] = value;
return;
}
int j = numberOfElements - 1;
while (j >= 0 && arr[j] > value) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = value;
}
public void displayQueue() {
for (int i = 0; i < numberOfElements; i++) {
System.out.print(arr[i] + " ");
}
System.out.println("");
}
}
public class MyPriorityQueue {
public static void main(String[] args) {
PQueue queue = new PQueue(10);
queue.insertSorted(8);
queue.insertSorted(5);
queue.insertSorted(10);
queue.insertSorted(2);
queue.displayQueue();
}
}