我在hackerearth 上研究哈希表,在那里我遇到了使用线性探测实现哈希表的代码。我对这段代码有两个疑问:-
1)为什么他们声明大小为 21(而不是大小为 20)的 hashTable 最多可容纳 20 个元素?
2) 在 Insert 函数中,如果在连续迭代后 index 的值与 Index 的初始值相同,while 循环是否会无限运行?
链接到hackerearth页面:- https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/
Code:-
//declaring a hashTable to hold not more than 20 elements
string hashTable[21];
int hashTableSize = 21;
//Insert function
void insert(string s)
{
//Compute the index using the hash function
int index = hashFunc(s);
/*Search for an unused slot and if the index will exceed the hashTableSize then roll back*/
// I have problem in this loop definition
while(hashTable[index] != "")
index = (index + 1) % hashTableSize;
hashTable[index] = s;
}