c - 使用 C 实现哈希表

标签 c arrays string file hashtable

我正在尝试读取一个名称文件,并根据这些名称的字母值将这些名称散列到一个位置。我已成功获取文件中每个名称的值,但在创建哈希表时遇到问题。我可以只使用常规数组并编写一个函数来将单词放在它们值的索引处吗?

while (fgets(name,100, ptr_file)!=NULL) //while file isn't empty
            {
            fscanf(ptr_file, "%s", &name); //get the first name
            printf("%s ",name); //prints the name
            int length = strlen(name); // gets the length of the name
            int i;
            for (i =0; i <length; i++) 
            //for the length of the string add each letter's value up
                {
                value = value + name [i];
                value=value%50;
                }
            printf("value= %1d\n", value);
            }

最佳答案

不,你不能,因为你会发生冲突,因此你必须将多个值考虑到单个散列。通常,您的散列并不是一个明智的实现——为什么将值的范围限制为 50?内存真的很稀疏,所以你不能拥有超过 50 个指针的字典吗?

我建议使用现有的 C 字符串哈希表实现,例如 this one从 2001 年开始。

关于c - 使用 C 实现哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27948307/

相关文章:

java - 缩放和旋转点数组

c++ - 动态分配对象数组

java - 如何将 6020494385.89982 字符串值转换为 double

c - 在C中使用scanf解析输入

c - 为什么巴塞尔问题总是显示相同的数字?

c - 最重要的位在左边吗?

php - 使用 PHP 从只读 Google 表格获取数据

c - 在 C 程序中包含源文件

java - 在java中将 int 1 转换为字符串 'one' 、 2 转换为 'two' 等时出现问题

c# - 如何在 C# 中维护字符串中引用的多个引号