我試圖實現鏈式哈希表中C.這是一個項目從一門課程,因此不會是完美的,但我不能讓它上班。哈希表的實現 - 裏面只有垃圾值
在我添加新值的哈希表的一切方法似乎不錯,但後來當我試圖找到在哈希表中某些價值似乎沒有什麼內部(或一些垃圾值)。我認爲散列函數工作正常,所以我不會發布它。下面是相關代碼:
// typedef a node for the linked list
typedef struct node
{
char* value;
struct node* next;
}
node;
// global variables
node* head = NULL;
node* lists[145000];
// this method inserts a new value into the hash table
bool insert_word(char* value, int index)
{
// inserting at the beginning of the list
node* new_node = malloc(sizeof(node));
new_node->value = value;
if (head == NULL)
{
head = new_node;
}
else
{
new_node->next = head;
head = new_node;
}
lists[index] = head;
return true;
}
// this method should check if the searched word
// is present in the hash table
bool check(const char* word)
{
int index = hash(word);
node* curr_node = lists[index];
while (curr_node != NULL)
{
if (strcmp(curr_node->value, word) == 0) // this never happens
{
return true;
}
curr_node = curr_node->next;
}
return false;
}
,因爲我一直是這樣掙扎了兩天,我希望得到任何幫助......謝謝:)
你應該與所有警告和調試信息(例如'GCC -Wall -g')編譯,你應該**使用調試器**(例如'gdb')。你真的無法避免使用調試器 – 2014-09-02 10:07:38
你不在上面的代碼塊中定義'node'。 – 2014-09-02 10:09:00
如何調用'bool insert_word(char * value,int index){}'? (在分配給node-> value之前,可能需要strdup()參數) – joop 2014-09-02 10:10:47