最小堆测试查找和删除操作

This commit is contained in:
free will
2021-05-05 22:21:51 +08:00
parent 6928973ccf
commit cf22dde2da
2 changed files with 25 additions and 11 deletions
+8
View File
@@ -57,6 +57,14 @@ func (h *CanBeUsedAddrHeap) IsExistAddr(addr CanBeUsedAddr) int{
}
return -1
}
func (h *CanBeUsedAddrHeap) IsExistAddrIP(ip string) int{
for i := 0; i < len(*h); i++ {
if (*h)[i].ip==ip {
return i
}
}
return -1
}
// 打印所有元素
func (h *CanBeUsedAddrHeap) PrintSelf() {
+17 -11
View File
@@ -44,18 +44,24 @@ func main() {
heap.Push(&h,addr5)
h.PrintSelf()
addrPop := heap.Pop(&h).(CanBeUsedAddr)
fmt.Println("pop ip: "+addrPop.ip)
h.PrintSelf()
addrPop = heap.Pop(&h).(CanBeUsedAddr)
fmt.Println("pop ip: "+addrPop.ip)
addrPop = heap.Pop(&h).(CanBeUsedAddr)
fmt.Println("pop ip: "+addrPop.ip)
addrPop = heap.Pop(&h).(CanBeUsedAddr)
fmt.Println("pop ip: "+addrPop.ip)
addrPop = heap.Pop(&h).(CanBeUsedAddr)
fmt.Println("pop ip: "+addrPop.ip)
fmt.Println("ip 3 exist: ",h.IsExistAddrIP("3"))
fmt.Println("ip 4 exist: ",h.IsExistAddrIP("4"))
fmt.Println("ip 0 exist: ",h.IsExistAddrIP("0"))
heap.Remove(&h,3)
h.PrintSelf()
//addrPop := heap.Pop(&h).(CanBeUsedAddr)
//fmt.Println("pop ip: "+addrPop.ip)
//h.PrintSelf()
//addrPop = heap.Pop(&h).(CanBeUsedAddr)
//fmt.Println("pop ip: "+addrPop.ip)
//addrPop = heap.Pop(&h).(CanBeUsedAddr)
//fmt.Println("pop ip: "+addrPop.ip)
//addrPop = heap.Pop(&h).(CanBeUsedAddr)
//fmt.Println("pop ip: "+addrPop.ip)
//addrPop = heap.Pop(&h).(CanBeUsedAddr)
//fmt.Println("pop ip: "+addrPop.ip)
//h.PrintSelf()
//addrServer := new(AddrServer)
//addrServer.start()