I have this code:
/*
* File: main.c
* Author: Danny
*
* Created on November 26, 2009, 11:59 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
/*node of the list which will store the words while "hanging" of the array*/
typedef struct ListNode {
char* word;
int size;
struct List *next;
} List;
typedef struct {
List** list;
int size;
} HashTable;
/*returns a initialized hash table*/
HashTable* createTable(int tableSize) {
int i = 0; /*loop iterator*/
HashTable* newTable = (HashTable*) malloc(sizeof (HashTable)); /*new hashtable pointer to be returned*/
newTable->list = (List**) malloc(sizeof (List*) * tableSize); /*malloc for the hashTable*/
newTable->size = tableSize;
/**
for (i = 0; i < tableSize; i++) {
newList[i].next = NULL;
newList[i].size = 0;
newList[i].word = NULL;
}
**/
/*assignment of the new hashTable's list to the new list created*/
//newTable->list = &newList;
return newTable; /*new HashTable returned*/
}
/*returns 1 if the hash table list at location "key" is empty*/
int HashKeyEmpty(HashTable* H, int key) {
if (H->list[key] == NULL) return 1;
return 0;
}
/*inserts a node to a list*/
void ListInsert(HashTable* H, int key, List* node) {
List* listPtr = H->list[key];
if (listPtr == NULL)
H->list[key] = node;
else {
// traverse to end
while (listPtr->next != NULL)
listPtr = listPtr->next;
listPtr->next = node;
}
}
/*prints the whole list nodes*/
void PrintList(List* list) {
List* listPtr = NULL;
listPtr = &list;
while(listPtr != NULL)
{
printf("%s ,",listPtr->word);
listPtr = listPtr->next;
}
if(list == NULL)
printf("Empty\n");
}
void HashInsert(HashTable* H, char *word) {
int key = 0;
int i = 0;
List* tempNode;
for (i = 0; i < strlen(word); i++)
key = (key * 31 + word[i]);
key = labs(key) % H->size;
List* newNode;
newNode = (List*) malloc(sizeof (List));
newNode->next = NULL;
newNode->word = (char*) malloc(strlen(word) + 1);
strcpy(newNode->word, word);
ListInsert(H, key, newNode);
}
int main(int argc, char** argv) {
FILE *fp;
char *word;
double number_of_words = 0;
double hashSize = 0;
int tableSize = 0;
int h;
int i;
fp = fopen("ispell.wordlist", "r");
//count the number of words
while (fscanf(fp, "%s", word) == 1) {
number_of_words++;
}
fclose(fp);
//approximately 20%
hashSize = (ceil(number_of_words * 0.01)* 100 + 100) * 0.2;
tableSize = (int) hashSize;
printf("%d \n", tableSize);
HashTable* hashTable = NULL;
hashTable = createTable(tableSize);
fp = fopen("ispell.wordlist", "r");
i=0;
while (fscanf(fp, "%s", word) == 1) {
HashInsert(hashTable, word);
i++;
}
fprintf("%s \n", hashTable->list[100]->word); <------ the problem
return (EXIT_SUCCESS);
}
how do I access this word that inside my hashtable?!