2017-04-05 126 views

回答

4

在您的解决方案的问题是,您使用的“线性探测”为插入操作,但你没有使用相同的方法来检索它。

首先的 - 我会改变你的强调存储来保存,而不是价值整体结构:

var hasharray [15]Item 

其次,我会改变的检索方法检查与计算的散列索引项的值,并经过通过一个迭代的项目之一,以找到实际的项目,如果有冲突:

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 
     } 
    } 
} 

在这里看到:https://play.golang.org/p/8JfTpbJcWx