谁能向我解释一下:我想在 GO 中实现一个优先级队列(接口实现从link获得,但优先级最低)
我的代码:
pq := make(PriorityQueue, 0)
pq.Push(&Item{value: 0, priority: 0})
heap.Init(&pq)
fmt.Println(heap.Pop(&pq).(*Item))
item := &Item{value: 1, priority: 10}
pq.Push(item)
item = &Item{value: 2, priority: 20}
pq.Push(item)
item = &Item{value: 3, priority: 5}
pq.Push(item)
fmt.Println(heap.Pop(&pq).(*Item))
fmt.Println(heap.Pop(&pq).(*Item))
fmt.Println(heap.Pop(&pq).(*Item))
// Output:
&{0 0 -1}
&{1 10 -1}
&{3 5 -1}
&{2 20 -1}
Run Code Online (Sandbox Code Playgroud)
为什么不输出:
&{0 0 -1}
&{3 5 -1}
...
Run Code Online (Sandbox Code Playgroud)