-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.go
105 lines (92 loc) · 1.8 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
/*
* @Name: Priority Sorted Queue Go
* @Author: Max Base
* @Date: 2022-11-14
* @Repository: https://github.com/basemax/PrioritySortedQueueGo
*/
package main
import "fmt"
type QueueItem struct {
Value interface{}
Priority int
}
type PriorityQueue struct {
front int
rear int
size int
// sorted 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(n)
func (q *PriorityQueue) Enqueue(value interface{}, priority int) {
if q.IsFull() {
return
}
q.rear++
q.items[q.rear] = QueueItem{
Value: value,
Priority: priority,
}
if q.front == -1 {
q.front = 0
}
// sort
for i := q.rear; i > 0; i-- {
if q.items[i].Priority > q.items[i-1].Priority {
q.items[i], q.items[i-1] = q.items[i-1], q.items[i]
}
}
}
// Dequeue, O(1)
func (q *PriorityQueue) Dequeue() interface{} {
if q.IsEmpty() {
return nil
}
item := q.items[q.front]
q.front++
return item.Value
}
// Peek, O(1)
func (q *PriorityQueue) Peek() interface{} {
if q.IsEmpty() {
return nil
}
return q.items[q.front].Value
}
// Size, O(1)
func (q *PriorityQueue) Size() 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 ""
}
str := ""
for i := q.front; i <= q.rear; i++ {
str += fmt.Sprintf("%v ", q.items[i].Value)
}
return str
}