0

我有一个包含大约 10k 个字符串的输入文件。我需要数一数。使用开放寻址,双重哈希的冲突。但是代码进入无限循环,即,它根本找不到任何空的地方来填充!(对于大于 1000 的输入)。

根据给我的问题,我们应该使用它。 第一个哈希:ASCII 第二个哈希的总和:除法即 mod sizeof(Prime)

我认为这与我的哈希函数有关。

我创建了一个结构数组,用于使用以下存储字符串。

struct table{
    char word[50];
};

然后创建一个数组,如下所示:

struct table*array=(struct table*)malloc(10000*sizeof(struct table));

以下是我使用的 2 个哈希函数:

int hash1(char* name) //This function calculates the ascii value
{
    int index=0;int i=0;
    for(;name[i]!='\0';i++)
    {
        index+=name[i];
    }
    return index;

}

int hash2(int index) //Calculates offset (mod with a large prime,Why do we do this?)
{
    int offset=index%1021;
    return offset;
}

下面是我的插入功能。

insert(char* name)
{
    int offset=0,index=0;
    index=hash1(name);//calculate the index from string
    index=index % 10000;//to keep index between 0-10k range
    while((array[index].word[0]!='\0')) // checking if space in array empty
    {
        collision+=1;    //global counter
        offset=hash2(index); //offset
        index=index+offset;
        index=index%10000;
    }
    strcpy(array[index].word,name);
    return array;

}

如何改进我的哈希函数,使其访问数组上的所有 10k 位置。?(或完全改变它)。

根据给我的问题,我们应该使用它。第一个哈希:ASCII 第二个哈希的总和:除法即 mod sizeof(Prime)

4

0 回答 0