tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
43
Task/Priority-queue/Go/priority-queue.go
Normal file
43
Task/Priority-queue/Go/priority-queue.go
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"container/heap"
|
||||
)
|
||||
|
||||
type Task struct {
|
||||
priority int
|
||||
name string
|
||||
}
|
||||
|
||||
type TaskPQ []Task
|
||||
|
||||
func (self TaskPQ) Len() int { return len(self) }
|
||||
func (self TaskPQ) Less(i, j int) bool {
|
||||
return self[i].priority < self[j].priority
|
||||
}
|
||||
func (self TaskPQ) Swap(i, j int) { self[i], self[j] = self[j], self[i] }
|
||||
func (self *TaskPQ) Push(x interface{}) { *self = append(*self, x.(Task)) }
|
||||
func (self *TaskPQ) Pop() (popped interface{}) {
|
||||
popped = (*self)[len(*self)-1]
|
||||
*self = (*self)[:len(*self)-1]
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
pq := &TaskPQ{{3, "Clear drains"},
|
||||
{4, "Feed cat"},
|
||||
{5, "Make tea"},
|
||||
{1, "Solve RC tasks"}}
|
||||
|
||||
// heapify
|
||||
heap.Init(pq)
|
||||
|
||||
// enqueue
|
||||
heap.Push(pq, Task{2, "Tax return"})
|
||||
|
||||
for pq.Len() != 0 {
|
||||
// dequeue
|
||||
fmt.Println(heap.Pop(pq))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue