Skip to content

Instantly share code, notes, and snippets.

@Lunanne
Created November 28, 2012 20:03
Show Gist options
  • Save Lunanne/4163801 to your computer and use it in GitHub Desktop.
Save Lunanne/4163801 to your computer and use it in GitHub Desktop.
Hashing code, stupid homework
typedef struct element
{
char * woord;
struct element * next;
} ELEMENT;
char* word;
ELEMENT * node;
ELEMENT * newNode
void readfile()
{
FILE * fp = fopen("woorden.txt","rb");
char tempBuffer[35];
while(fgets(tempBuffer,sizeof(tempBuffer),fp)!=NULL)
{
//we found a word.Let's assume its shorter than 35 characters and we can safe some space
int length = strlen(tempBuffer);
word = malloc(length*sizeof(char));
strncpy(word,tempBuffer,length);
if(word[length-1]=='\n')
word[length-1] = '\0';
else
word[length]='\0';
//put it in the hashlist
ELEMENT newWord;
newWord.woord=word;
unsigned int hashcode = hashCode(word);
int index = hashcode%HASHARRAYSIZE;
node = hashArray[index];
if(node!=NULL)
{
while(node->next!=NULL)
{
node = node->next;
}
newNode=malloc(sizeof(ELEMENT));
newNode=&newWord;
node->next=newNode;
newNode->next =NULL;
}
else
{
hashArray[index] = malloc(sizeof(ELEMENT));
hashArray[index] = newWord;
hashArray[index]->next=NULL;
}
}
fclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment