Hey guys
I don't really consider myself the best of a programmer but i'm currently working on a difficult program, or what i consdier difficult.
There's one hitch though, i run this through the command line ./list -t words.txt < awords.txt. Basically it gets awords.txt and enters it into a trie structure and compares it with words.txt to see if their present or not.
If i compare them both and they don't have any words, it seg faults and obviously crashes... i'm really worn out and can't think straight right now so i would greatly appreciate the help. From what i understand it seg faults in trie_find.
If you want any more code, just ask. Cheers
Code:
letterTypePtr trie_insert(char *word,ListTypePtr list, letterTypePtr node, int len)
{
char character[2];
letterTypePtr prevLetter = NULL;
--len;
if(len != 0)
{
node = trie_insert(word,list, node, len);
prevLetter = node;
node = node->downLet;
}
character[0] = word[len];
character[1] = '\0';
if(node == NULL && prevLetter == NULL)
{
list->head = add_node(character);
return list->head;
}
if(prevLetter != NULL && node == NULL)
{
prevLetter->downLet = add_node(character);
return prevLetter->downLet;
}
if(prevLetter != NULL && node != NULL)
{
while (node != NULL)
{
if (strcmp(node->character, character) == 0 )
return node;
prevLetter = node;
node = node->nextLet;
}
prevLetter->nextLet = add_node(character);
return prevLetter->nextLet;
}
if(prevLetter == NULL && node != NULL)
{
while (node != NULL)
{
if (strcmp(node->character, character) == 0 )
return node;
prevLetter = node;
node = node->nextLet;
}
prevLetter->nextLet = add_node(character);
return prevLetter->nextLet;
}
}
/* just gets memory for the node */
letterTypePtr add_node(char *word)
{
letterTypePtr newLetter;
newLetter = malloc(sizeof(letterType));
assert(newLetter);
newLetter->character = malloc(sizeof(char) * (strlen(word) + 1));
strcpy(newLetter->character, word);
newLetter->downLet = NULL;
newLetter->nextLet = NULL;
return newLetter;
}
/* trie find */
letterTypePtr trie_find(char *word, letterTypePtr node, int len)
{
letterTypePtr prevLetter;
char character[2];
--len;
if(len != 0)
{
node = trie_find(word, node, len);
prevLetter = node;
node = node->downLet;
}
character[0] = word[len];
character[1] = '\0';
if (node == NULL)
{
return NULL;
}
while (node != NULL)
{
if (strcmp(node->character, character) == 0)
{
return node;
}
node = node->nextLet;
}
return NULL;
}
/* loadWords
*/
int loadWords(ListTypePtr list)
{
char word[50];
while (fscanf(stdin,"%s",word) != EOF)
{
trie_insert(word,list, list->head, strlen(word));
list->count++;
}
return 1;
}