- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.java
More file actions
Latest commit
64 lines (52 loc) · 1.49 KB
/
Copy pathPriorityQueue.java
File metadata and controls
64 lines (52 loc) · 1.49 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
62
63
publicclassPriorityQueue<V> implementsQueueInterface<V>{
privateNodeBase<V>[] queue;
privateintcapacity, currentSize;
//TODO Complete the Priority Queue implementation
// You may create other member variables/ methods if required.
@SuppressWarnings("unchecked")
publicPriorityQueue(intcapacity) {
queue=newNodeBase[capacity];
currentSize=0;
this.capacity=capacity;
}
publicintsize() {
returncurrentSize;
}
publicbooleanisEmpty() {
returncurrentSize<=0;
}
publicbooleanisFull() {
returncurrentSize>=capacity;
}
publicvoidenqueue(Node<V> node) {
if (isFull())
return;
inti;
for (i=0;i<currentSize;i++)
if (node.getPriority()<queue[i].getPriority())
break;
for (intj=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
publicNodeBase<V> dequeue() {
if (isEmpty())
returnnull;
NodeBase<V> R=queue[0];
for (inti=1;i<currentSize;i++)
queue[i-1]=queue[i];
currentSize--;
returnR;
}
publicvoiddisplay () {
if (this.isEmpty()) {
System.out.println("Queue is empty");
}
for(inti=0; i<currentSize; i++) {
queue[i+1].show();
}
}
}