/**
* Implements a dictionary's functionality.
*/
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <cs50.h>
#include <string.h>
#include "dictionary.h"
//define a trie node
typedef struct _node{
bool isword;
struct _node *lett[27];
}
tnode;
tnode *head = NULL;
unsigned int word = 0;
int hcode(char character)
{
if (isalpha(character)){
return tolower(character) - 97;
}
else if(character + 0==39){
return 26;
}
return false;
}
char dehash(int character)
{
if (character<26 && character >= 0){
return character + 97;
}
else if(character ==26){
return 39;
}
return false;
}
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word)
{
// TODO
return false;
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
char str[45];
bool load(const char *dictionary)
{
FILE *dc = fopen(dictionary, "r");
if(dc == NULL){
return false;
}
head = malloc(sizeof(tnode));
tnode* cursor = head;
while (fscanf(dc, "%s", str) != EOF){
word++;
cursor = head;
if(head == NULL){
return false;
}
for(int b = 0;b<strlen(str);b++){
printf("0");
if(cursor->lett[hcode(b)]==NULL){
tnode* newnode = malloc(sizeof(tnode));
if(newnode == NULL){
return false;
}
*newnode = (tnode) {false,{NULL}};
cursor->lett[hcode(b)] = newnode;
}
printf("1");
if(b == strlen(str)-1){
cursor->lett[hcode(b)]->isword = true;
}
cursor=cursor->lett[hcode(b)];
}
printf("2\n");
}
fclose(dc);
// TODO
return true;
}
I've tried debugging and figuring out the problem. I'm trying to implement load. In my trie, I can only go in one node deep from the header node without suffering a seg fault. I don't know why the node in the array is null. Could I have some help?