【发布时间】:2016-03-03 04:42:56
【问题描述】:
我正在尝试更准确地创建一个哈希表,一个链表数组,我已经为链表使用了 add 函数并且工作正常,但是在尝试对哈希表进行此操作时会产生分段错误。
void create_HTable(FILE* book, list* hashTable[1009])
{
char word[20];
read(word,book);
while(strcmp(word,"EOF")!=0)
{
int hash_number = hash(word,1009);
list* node = hashTable[hash_number];
node = add(node,word);
read(word, book);
}
}
list *add(list *old_list, char new_word[20])
{
//this is a special case when the head of the list is empty
if(old_list==NULL)
{
return insert(new_word,NULL);
}
else
{
list *new_list = old_list;
if (doesExist(new_list, new_word, true) == 0)
{
while (new_list->next !=NULL)
{
new_list = new_list->next;
}
new_list->next=insert(new_word,NULL);
}
return old_list;
}
}
这适用于普通的链表,但是当我尝试在我的哈希表函数中重用它时它会崩溃。
【问题讨论】:
-
为什么要和
"EOF"比较?该文件不包含任何形式的"EOF",它只是一些函数返回的一个整数,表示您在文件末尾之后已经走了。
标签: c hash linked-list