-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.kt
88 lines (72 loc) · 2.33 KB
/
PriorityQueue.kt
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.ds.implementation.priorityqueue.array
import com.ds.implementation.priorityqueue.common.PriorityQueueNode
import com.ds.implementation.priorityqueue.common.PriorityQueueOperation
class PriorityQueue<T>(private val size: Int = 10) : PriorityQueueOperation<T> {
private val priorityQueue = Array<PriorityQueueNode<T>?>(size) { null }
private var front = -1
private var rear = -1
override fun enqueue(value: PriorityQueueNode<T>): Boolean {
if (isFull()) {
printWaringMessage("Failed to enqueue $value as Queue is full")
return false
}
if (front == -1) {
front++
rear++
priorityQueue[front] = value
return true
}
for(i in rear downTo front) {
if (value.priority > priorityQueue[i]?.priority!!) {
for (j in (front) downTo i)
{
priorityQueue[j+1] = priorityQueue[j]
}
priorityQueue[i] = value
break
}
}
priorityQueue[++rear] = value
return true
}
override fun dequeue(): Boolean {
if (isEmpty()) {
printWaringMessage("Failed to dequeue as Queue is empty")
return false
}
front++
return true
}
override fun peek(): PriorityQueueNode<T>? {
if (isEmpty()) {
printWaringMessage("Failed to Peek as Queue is empty")
return null
}
return priorityQueue[front]
}
override fun isEmpty(): Boolean {
if (front == -1 || front > rear) {
front = -1
rear = -1
return true
}
return false
}
override fun isFull(): Boolean = rear >= size - 1
override fun print(msg: String?) {
msg?.let { println(it) }
println("Front of Queue $front")
println("Rear of Queue $rear")
if (isEmpty()) {
println("Queue is empty")
return
}
for (index in front .. rear) {
if (priorityQueue[index] != null)
println("Position $index Entry -> ${priorityQueue[index]?.value} and Priority ${priorityQueue[index]?.priority}")
}
}
private fun printWaringMessage(msg: String?) {
msg?.let { println(it) }
}
}