-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.go
121 lines (108 loc) · 2.19 KB
/
PriorityQueue.go
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* @Name: Priority Unsorted Queue Go
* @Author: Max Base
* @Date: 2022-11-12
* @Repository: https://github.com/basemax/PriorityUnsortedQueueGo
*/
package main
import "fmt"
type QueueItem struct {
Value interface{}
Priority int
}
type PriorityQueue struct {
front int
rear int
size int
// unsorted list
items []QueueItem
}
// Create, O(1)
func NewPriorityQueue(size int) *PriorityQueue {
return &PriorityQueue{
front: -1,
rear: -1,
size: size,
items: make([]QueueItem, size),
}
}
// isFull, O(1)
func (q *PriorityQueue) isFull() bool {
return q.rear == q.size-1
}
// isEmpty, O(1)
func (q *PriorityQueue) isEmpty() bool {
return q.front == -1 || q.front > q.rear
}
// Enqueue, O(1)
func (q *PriorityQueue) Enqueue(value interface{}, priority int) {
if q.isFull() {
fmt.Println("Queue is full!")
return
}
if q.front == -1 {
q.front = 0
}
q.rear = q.rear + 1%q.size
q.items[q.rear] = QueueItem{
Value: value,
Priority: priority}
}
// Dequeue, O(n)
func (q *PriorityQueue) Dequeue() interface{} {
if q.isEmpty() {
fmt.Println("Queue is empty!")
return nil
}
max := q.items[q.front]
maxIndex := q.front
i := q.front + 1%q.size
for ; i <= q.rear; i++ {
if q.items[i].Priority > max.Priority {
max = q.items[i]
maxIndex = i
}
}
for maxIndex < q.rear {
q.items[maxIndex] = q.items[maxIndex+1]
maxIndex = maxIndex + 1%q.size
}
q.rear--
return max.Value
}
// Peek, O(n)
func (q *PriorityQueue) Peek() interface{} {
if q.isEmpty() {
fmt.Println("Queue is empty!")
return nil
}
max := q.items[q.front]
i := q.front + 1%q.size
for ; i <= q.rear; i++ {
if q.items[i].Priority > max.Priority {
max = q.items[i]
}
}
return max.Value
}
// Count, O(1)
func (q *PriorityQueue) Count() int {
return q.rear - q.front + 1
}
// Clear, O(1)
func (q *PriorityQueue) Clear() {
q.front = -1
q.rear = -1
}
// ToString, O(n)
func (q *PriorityQueue) ToString() string {
if q.isEmpty() {
return "Queue is empty!"
}
result := ""
i := q.front
for ; i <= q.rear; i++ {
result += fmt.Sprintf("%v ", q.items[i].Value)
}
return result
}