-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpriorityQueue.go
More file actions
52 lines (43 loc) · 928 Bytes
/
priorityQueue.go
File metadata and controls
52 lines (43 loc) · 928 Bytes
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
package queue
import (
"fmt"
)
// PriotityQueue queue with priotity
type PriorityQueue struct {
items []item
}
type item struct {
Value interface{}
Priority int
}
func (p *PriorityQueue) Enqueue(element interface{}, priority int) {
ele := item{}
ele.Value = element
ele.Priority = priority
p.items = append(p.items, ele)
for i := len(p.items) - 1; i > 0; i-- {
if p.items[i-1].Priority < priority {
p.items[i-1], p.items[i] = p.items[i], p.items[i-1]
}
}
}
func (p *PriorityQueue) Dequeue() interface{} {
firstValue := p.items[0].Value
p.items = p.items[1:]
return firstValue
}
func (p *PriorityQueue) Front() interface{} {
return p.items[0].Value
}
func (p *PriorityQueue) IsEmpty() bool {
return len(p.items) == 0
}
func (p *PriorityQueue) Size() int {
return len(p.items)
}
func (p *PriorityQueue) Clear() {
p.items = []item{}
}
func (p *PriorityQueue) Print() {
fmt.Printf("%#v\n", p)
}