-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.61 KB
/
main.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
package main
import (
"container/heap"
"fmt"
)
// Task represents a task in the task management system
type Task struct {
ID int
Priority int
Description string
}
// PriorityQueue implements a priority queue for tasks
type PriorityQueue []*Task
// Len returns the length of the priority queue
func (pq PriorityQueue) Len() int {
return len(pq)
}
func (pq PriorityQueue) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq[i].Priority > pq[j].Priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
// Push adds a task to the priority queue
func (pq *PriorityQueue) Push(task interface{}) {
item := task.(*Task)
*pq = append(*pq, item)
}
// Pop removes and returns the highest priority task from the priority queue
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[:n-1]
return item
}
func main() {
// Create a priority queue and add tasks
pq := make(PriorityQueue, 0)
heap.Init(&pq)
task1 := &Task{ID: 0, Priority: 3, Description: "Do A"}
heap.Push(&pq, task1)
task2 := &Task{ID: 1, Priority: 1, Description: "Do B"}
heap.Push(&pq, task2)
task3 := &Task{ID: 2, Priority: 2, Description: "Do C"}
heap.Push(&pq, task3)
fmt.Println("Length of the priority queue is:", pq.Len())
// Process tasks in highest priority order
for pq.Len() > 0 {
task := heap.Pop(&pq).(*Task)
fmt.Printf("Processing task with Priority: %d, ID: %d, Description: '%s' \n", task.Priority, task.ID, task.Description)
}
fmt.Println("Length of the priority queue is:", pq.Len())
}