package main
import "container/heap"
type IntHeap []int
func (h IntHeap) Len() int{return len(h)};
func (h IntHeap) Less(i,j int)bool{return h[i]<h[j]}
func(h IntHeap) Swap(i,j int){h[i],h[j]=h[j],h[i]}
func(h *IntHeap) Push(x interface{}) {
*h=append(*h,x.(int))
}
func(h *IntHeap) Pop() interface{}{
n := len(*h)
x:= (*h)[n-1]
(*h)=(*h)[0:n-1]
return x
}
func main(){
h:=&IntHeap{1,2,3,4,9,23,2,54,2345,34,2}
heap.Init(h)
for h.Len()>0{
println(heap.Pop(h).(int))
}
}
go的优先队列
最新推荐文章于 2024-10-10 16:49:19 发布