Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用线性探测插入哈希表_C++_Hash_Linear Probing - Fatal编程技术网

C++ 使用线性探测插入哈希表

C++ 使用线性探测插入哈希表,c++,hash,linear-probing,C++,Hash,Linear Probing,我在研究hackerearth上的哈希表,在那里我遇到了用线性探测实现哈希表的代码。我对这一准则有两个疑问:- 1) 为什么他们声明大小为21(而不是20)的哈希表最多可以容纳20个元素 2) 在Insert函数中,当循环无限运行时,如果在连续的迭代之后,索引的值变得和索引的初始值相同,不是吗 链接至黑客主页:- 第1点: 这是因为搜索功能。请看链接的搜索功能 void search(string s) { //Compute the index using th

我在研究hackerearth上的哈希表,在那里我遇到了用线性探测实现哈希表的代码。我对这一准则有两个疑问:-

1) 为什么他们声明大小为21(而不是20)的哈希表最多可以容纳20个元素

2) 在Insert函数中,当循环无限运行时,如果在连续的迭代之后,索引的值变得和索引的初始值相同,不是吗

链接至黑客主页:-


第1点:
这是因为搜索功能。请看链接的搜索功能

    void search(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
        while(hashTable[index] != s and hashTable[index] != "")
            index = (index + 1) % hashTableSize;
        //Check if the element is present in the hash table
        if(hashTable[index] == s)
            cout << s << " is found!" << endl;
        else
            cout << s << " is not found!" << endl;
    }
void搜索(字符串s)
{
//使用哈希函数计算索引
int index=hashFunc(s);
//搜索未使用的插槽,如果索引将超过哈希表大小,则回滚
while(hashTable[index]!=s和hashTable[index]!=“”)
索引=(索引+1)%hashTableSize;
//检查哈希表中是否存在该元素
if(哈希表[索引]==s)

也许,问题2只是问题1的答案。(存储最大数量的元素以确保元素必须完成否则可能是无止境的循环所需的更多元素。)否则,我也看不到原因。(作者应该提到这个(或另一个)原因,IMHO。)
    void search(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
        while(hashTable[index] != s and hashTable[index] != "")
            index = (index + 1) % hashTableSize;
        //Check if the element is present in the hash table
        if(hashTable[index] == s)
            cout << s << " is found!" << endl;
        else
            cout << s << " is not found!" << endl;
    }