【发布时间】:2011-12-31 19:23:17
【问题描述】:
所以我正在尝试实现一个哈希表,它将对包含单词的结构进行哈希处理。
结构将与此类似:
#ifndef HASHTABLE_H
#def HASHTABLE_H
typedef int (*HashFunctionT) (char* string, int upperbound);
struct node_
{
char * word;
struct node * next;
}
typedef struct node_ * node;
struct nodehash_
{
int size;
struct node * hash[100];
}
typedef struct nodehash_ * nodehash;
Hashtable createHashTable();
void addtohash(node list, nodehash hash);
#endif
我希望哈希函数可以像这样工作:
#include "hashtable.h"
int hashFunction(char *word, int hashTableSize)
{
int length = strlen(word);
int h = 0;
int i;
for(i = 0; i<length; i++)
{
h=31 *h + word[i];
}
return h % hashTableSize;
};
nodehash createHashtable()
{
nodehash hashtable;
hashtable = malloc(sizeof(struct nodehash_));
hashtable->size = 100;
hashtable->hash = malloc(100 * sizeof (node));
int i;
for (i = 0; i < hashtable->size; i++)
{
hashtable->table[i] = NULL;
}
return hashtable;
};
void addtohash(node list, nodehash hashtable)
{
int nodehashnumber;
nodehashnumber = hashfunction(list->word, hash->size);
hashtable->hash[nodehasnumber] = list;
};
主函数看起来像这样(假设已经创建并填充了节点结构的链表)。
int main()
{
nodehash hashtable = createhashtable();
node nodelist;
/* here the nodelist would be created and filled and such and such*/
while (nodelist->next != NULL)
{
addtohash(nodelist, hashtable);
}
return;
}
假设不可能有冲突,因为每个要散列的单词都会不同。
基本上,我想知道我是否遗漏了明显的错误或逻辑缺陷。
任何帮助将不胜感激。
谢谢。
【问题讨论】:
-
codereview.stackexchange.com 会不会更好?
-
哦,我什至不知道它的存在...感谢您告诉我!
-
你不能仅仅因为每个单词不同就假设不会有冲突。您的哈希函数可以为多个不同的输入产生相同的输出。
-
另外,如果你假设没有冲突,那么你为什么要使用带有 next 指针的节点结构呢?如果不考虑冲突,您可以只使用 char* 数组
-
所以这都是实现 LRU 缓存的大型项目的一部分......基本上,我们得到一个 .txt 文件作为输入,我们需要对文件进行标记,并使用搜索文件比 O(n^2) 时间更好的东西(所以基本上,线性搜索是不可能的,我们鼓励使用哈希表)。 LRU 缓存是包含令牌的,并且是动态的:也就是说,用户指定自己的缓存大小。