0
 /**
 * dictionary.c
 *
 * Computer Science 50
 * Problem Set 5
 *
 * Implements a dictionary's functionality.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"

unsigned int size_int = 0;
struct unit;
typedef struct tries
{
    struct unit alphabet[26];
}
tries;
struct unit
{
    bool check;
    struct _tries* next;
};
struct tries * root_node = NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char * word)
{
    int i, j;
    struct tries * temp_node = (struct tries *)malloc(sizeof(struct tries));
    for ( i = 0, j = strlen(word), temp_node = root_node; i < j; i++)
    {
        int l = *(word + i) > 91 ? *(word + i): *(word + i) + 32;
        if ( temp_node->alphabet[l - 97]->next != NULL && *(word + i + 1) != '\0')
        {
            temp_node = temp_node->alphabet[l - 97]->next;
        }
        else if (l == '\0') 
        {
           l = *(word + i - 1) > 91 ? *(word + i - 1): *(word + i - 1) + 32;
           return temp_node->alphabet[l - 97]->check;
        }
    }
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    root_node = (struct tries*)calloc(1, sizeof(struct tries));
    //this is being done using the tries
    int i, j, k;
    char* word = (char*)malloc(46);
    struct tries* cur_node = (struct tries*)malloc(sizeof(struct tries));
    FILE* dic = fopen(dictionary, "r");
    for (cur_node = root_node; feof(dic); )
    {   
        int l;
        for (i = 0, l = strlen(word); ;)
        {
            j = fgetc(dic);
            j = (j >= 91) ? j: j + 32;
            cur_node->(alphabet[j - 97])->check = true;
            if ((k = fgetc(dic)) != '\0')
            {
                cur_node->(alphabet[j - 97])->next = (tries*)malloc(sizeof(tries));
                cur_node = cur_node->(alphabet[j - 97])->next;
                j = k;
            }
            else
            {
                cur_node->(alphabet[j - 97])->next = NULL;
                cur_node->(alphabet[j - 97]->check) = true;
                size_int++;
            }
        }
    }
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // this would return the no. of worlds in the loaded dictionary
    return size_int;
}

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 *
 */
void un_load(struct tries * temp_node)
{
    int i;
    for (int i = 0; i < 26; i++)
    {
        if (temp_node->alphabet[i]->next != NULL)
        {
            un_load(temp_node->alphabet[i]->next);
        }
    }
    if ( i == 26)
    {
        free(temp_node);
    }
    return;
}

bool unload(void) // returns false for thr successful unload
{   
    un_load(root_node);
    return true;
}

enter image description here Please take a look.Anybody? Thank you.

1

By using 2 structs that reference each other, you've made it much more complicated than need be (and you won't be able to compile it because of the circular reference).

I'd suggest simplifying it to use only one struct, as given in the pset's walkthrough:

typedef struct node
{
    bool is_word;
    struct node *children[27];  
}
node;

node *root;
1
  • Thank you. I have started again. Isn't there any way to declare two structures which have struct pointer to eact other as the element. I read some answers on stackexchange and the code attached in the question is as per that. – Shaw Ankush Jan 5 '17 at 5:16

You must log in to answer this question.

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