I am working on SPELLER and am stuck on the load function. I have no errors the way it's written but the problem is in this code's location.
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
node *alphaArray[26] = {NULL};//I need this to only happen once. How to make this happen
I need to only call both once and then use both in the insert function over and over as I continue to call insert to load the dictionary's words into a hash table (alphaArray). I've tried passing alphaArray to the function and I've also tried defining it and neither seem to work.
Here is the complete code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LENGTH 45
//Prototypes
bool load(const char *dictionary);
int findPositionInAlphabet(const char *buffer);
void insert(int key, const char *buffer);
//Prototypes
int main(void){
char *dictionary = "dictionaries/large";
load(dictionary);
}//End main function
bool load(const char *dictionary){
FILE *dFile = fopen(dictionary, "r");
if(dFile == NULL){
return false;
}else{
char *word = " ";
while(fscanf(dFile, "%s", word) != EOF){
int positionInAlphabet = findPositionInAlphabet(word);
insert(positionInAlphabet, word);
}
}
return true;
}//End load function
int findPositionInAlphabet(const char *buffer){
return tolower(buffer[0]) - 'a';
}//End findPositionInAlphabet function
void insert(int key, const char *buffer){
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
}
node;
node *alphaArray[26] = {NULL};//I need this to only happen once. How to make this happen
node *newptr = malloc(sizeof(node));
if (newptr == NULL){ return; }
strcpy(newptr->word, buffer);
newptr->next = NULL;
if (alphaArray[key] == NULL){ alphaArray[key] = newptr;
}else{ node *predptr = alphaArray[key];
while (true){
if (predptr->next == NULL){
predptr->next = newptr;
break;
}
predptr = predptr->next;
}
}
}//End insert function