2017-04-05 08:13
浏览 151


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 :

图片转代码服务由CSDN问答提供 功能建议

我正在尝试实现一个哈希程序,我使用线性探测进行了插入并解决了冲突。 当我尝试取回值时,由于使用线性探测来解决冲突,我得到了不同的值。


  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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 {
            } else {
                index = 0

    See here:

    解决 无用
    打赏 举报

相关推荐 更多相似问题