小编Jus*_*Jus的帖子

GO中的优先级队列

谁能向我解释一下:我想在 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)

algorithm priority-queue go

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

algorithm ×1

go ×1

priority-queue ×1