-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.java
More file actions
executable file
·61 lines (47 loc) · 1.56 KB
/
Copy pathPriorityQueue.java
File metadata and controls
executable file
·61 lines (47 loc) · 1.56 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
53
54
55
56
57
58
59
60
61
package com.codeWithArsalon.LinearDS;
import java.util.Arrays;
public class PriorityQueue {
//priority queue using an array
private int [] items = new int[5];
private int [] newItems;
private int count;
public void enqueue(int item) {
resizeIfRequired();
int i = shiftItemsToInsert(item);
items[i] = item; //inserting at i + 1 position (next right spot)
count++;
}
private int shiftItemsToInsert(int item) {
int i;
for (i = count - 1; i >= 0; i--) { //sorting algorithm
if (items[i] < item) //smallest numbers to the right
items[i + 1] = items[i]; //shifting "copying" items right to make space to insert value at index
else
break;
}
return i + 1; //to return exact index
}
private void resizeIfRequired() {
if(isFull()) {
newItems = new int[count * 2]; //creating a 2x larger array
for (int i = 0; i < count; i++)
newItems[i] = items[i]; //copying values at each index into the new array
items = newItems;
}
}
public int dequeue(){
if(isEmpty())
throw new IllegalStateException();
return items[--count]; //changes count but doesn't remove item from array, could use circular array/shifting
}
public boolean isEmpty(){
return count == 0 ;
}
private boolean isFull() {
return count == items.length;
}
@Override
public String toString(){
return Arrays.toString(items);
}
}