-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.java
64 lines (52 loc) · 1.49 KB
/
PriorityQueue.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
52
53
54
55
56
57
58
59
60
61
62
63
public class PriorityQueue<V> implements QueueInterface<V>{
private NodeBase<V>[] queue;
private int capacity, currentSize;
//TODO Complete the Priority Queue implementation
// You may create other member variables/ methods if required.
@SuppressWarnings("unchecked")
public PriorityQueue(int capacity) {
queue=new NodeBase[capacity];
currentSize=0;
this.capacity=capacity;
}
public int size() {
return currentSize;
}
public boolean isEmpty() {
return currentSize<=0;
}
public boolean isFull() {
return currentSize>=capacity;
}
public void enqueue(Node<V> node) {
if (isFull())
return;
int i;
for (i=0;i<currentSize;i++)
if (node.getPriority()<queue[i].getPriority())
break;
for (int j=currentSize;j>i;j--)
queue[j]=queue[j-1];
queue[i]=node;
currentSize++;
}
// In case of priority queue, the dequeue() should
// always remove the element with minimum priority value
public NodeBase<V> dequeue() {
if (isEmpty())
return null;
NodeBase<V> R=queue[0];
for (int i=1;i<currentSize;i++)
queue[i-1]=queue[i];
currentSize--;
return R;
}
public void display () {
if (this.isEmpty()) {
System.out.println("Queue is empty");
}
for(int i=0; i<currentSize; i++) {
queue[i+1].show();
}
}
}