0

I am still stuck on pset5 speller.c. I am trying to implement unload via tries. I have tried several different codes which were not giving the correct output, but the last one I did could not even compile. Please I need suggestions on how to implement the unload function effectively. Please take a look at my code and let me know what am doing wrong. Please help. Thank you.

/**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
#define ALPHABETS 27

typedef struct node
    {
        bool is_word;
        struct node* children[ALPHABETS];
    }node;
    node* root = NULL;
    int counter =0;
   node* cursor = root;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    // TODO
    node* current = root;
    int w_index;

    int w_len = strlen(word);
    for(int i = 0; i < w_len;i++)
    {
        if(isupper(word[i]))
        w_index = (tolower(word[i])) - 'a';
        else
        w_index = word[i] - 'a';
         if(current->children[w_index] == NULL)
         {
         current->is_word = false;
         return false;
         }
         else
        current = current->children[w_index];

    }
    if(current->is_word == true)
    return true;
    else
    return false;

    return true;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    // TODO


    char d_word[LENGTH +1];
    root = (node*)malloc(sizeof(node));
    if(root)
    {
        int i;
        root->is_word = false;
        for(i = 0; i < ALPHABETS; i++)
       root->children[i] = NULL;
    }

    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open %s.\n", dictionary);
        unload();
        return 1;
    }

    int index;
        while(fscanf(file, "%s", d_word)!= EOF)
        {
            node*trav = root;
            int length = strlen(d_word);
           for(int j = 0; j < length; j++)
            {
                index = d_word[j] - 'a';

                    if(trav->children[index] == NULL)
                        trav->children[index] = malloc(sizeof(node));

                    trav = trav->children[index];
            }
            trav->is_word = true;
        counter++;
        }

    fclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // TODO
    return (counter);
}


/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void)
{
    // TODO

node* temp;

    for(int i = 0; i < ALPHABETS; i++)
    {

        if (cursor->children[i] != NULL)
        {
        temp = cursor;
        cursor = cursor->children[i];
        unload();
        }

        else
        free(temp);

    }
    return true;
}
1
  • What error message are you getting?
    – ronga
    Oct 28 '16 at 7:01
0

You can't assign one var to another as initialization of a global var when it is created. You can only assign values that are constants. The reason is that the value that is being used to initialize the global variable will be hardcoded into the compiled code. Assigning one var to another is something that can only be done at run time, but can't be done at compile time. However, to be clear, you could make the assignment inside of a function or in main.

However, since you previously set root to NULL, you could also set cursor to NULL too. It's the same as what you want to do.

Of course, the seg fault that's coming is a totally different issue. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .