我在尝试理解和实现使用线性探测的哈希表。在寻找一种实现方式时,我发现了这段代码用于搜索方法:
struct DataItem *search(int key) {
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
我不明白的是如果哈希数组中的所有条目都已被一个键占用,那么 while 循环不应该变成无限循环吗?还是这永远不会发生?我有所遗漏吗?
%SIZE
,使得int initialHashIndex = hashCode(key)%SIZE;
(哈希码计算通常与当前哈希表大小无关)成为可能。 - chux - Reinstate Monica