douyanpeng0748 2017-04-05 08:13
浏览 189
已采纳

使用线性探测解决冲突后,如何从哈希表中检索值?

I am trying to implement a hash program in go, I did insertion and resolved collisions using linear probing. When I'm trying to retrieve values back, i'm getting different values as I used linear probing to fix collisions.

This is my program : https://play.golang.org/p/7Pmqu6A313

  • 写回答

1条回答 默认 最新

  • dongzhe3573 2017-04-05 08:33
    关注

    The problem in your solution is that you are using "linear probing" for insert operation, but you are not using the same approach for retrieving it.

    First of all - I would change your underlining storage to keep whole struct instead of value:

    var hasharray [15]Item
    

    Second, I would change the retrieve method to check value of the item with calculated hash index, and after that iterate items one by one to find actual item if there was a collision:

    func retrieve(key string) {
        index := hashmethod(key)
        found := false
        for !found {
            item:= hasharray[index];
            if key == item.key {
             found = true;
             fmt.Println(index, item)
            } else if index != size-1 {
                index++
            } else {
                index = 0
            }
        }   
    }
    

    See here: https://play.golang.org/p/8JfTpbJcWx

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 C# TCP服务端,客户端退出后,不断有数据进来
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?