0

I have a problem when I use check50 in speller pset

enter image description here

It give me errors in all checks

But when i tried my code in cs50 ide and compare my result to the staff or key result it gives no errors

enter image description here

and that is my code:

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

#include "dictionary.h"

// Represents number of buckets in a hash table
#define N 26

// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1]; // +1 for \0
struct node *next;
}
node;



// Hashes word to a number between 0 and 25, inclusive, based on its first 
letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a'; // if letter is A  so change to lower => a 
,then subtruct 97 so it will be 0
}



// Loads dictionary into memory, returning true if successful else false

node *HEAD = NULL;// for fisrt element

// Represents a hash table
node *hashtable[N];

unsigned int n = 0;

bool load(const char *dictionary)
{


// Initialize hash table
for (int i = 0; i < N; i++)
{
    hashtable[i] = NULL;
}

// Open dictionary
FILE *dic = fopen(dictionary, "r");
if (dic == NULL)
{
    unload();
    return false;
}

// buffer for a word
char buffer[LENGTH + 1];
unsigned int check = 0;


// Insert words into hash table
while (fscanf(dic, "%s", buffer) != EOF) // read one string %s and put in buffer
{
    node *new_node = NULL;
    node *p = NULL;
    //create indvidual node
    new_node = malloc(sizeof(node));
    if (!new_node)
    {
        unload();
        return false;
    }
    else
    {
        strcpy(new_node -> word, buffer);
        new_node -> next = NULL;
    }

    unsigned int index = hash(new_node -> word);


    //make linked list

    // if head is still null (i.e. list is empty)
    if (HEAD == NULL)
    {
        HEAD = new_node;
    }
    else //list is not empty
    {

        if (index == check){

            check = index;
            /*
            //add element form end
            p = HEAD; // let p equals to head
            while (p -> next != NULL) // iterate until p is the last element
            {
                p = p->next;
            }
            p -> next = new_node; // add the node to the end of the linked list*/

            //add element from beginnig
            new_node -> next = HEAD;
            HEAD = new_node;

            hashtable[index] = HEAD;

        }
        else
        {

            HEAD = NULL;
            //tmpHEAD = NULL;
            HEAD = new_node;
            check = index;

        }

    }

    n++;
}




// Close dictionary
fclose(dic);

// Indicate success
return true;
}



// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{

/*display(hashtable[0]);
printf("\n");
display(hashtable[1]);
printf("\n");
display(hashtable[2]);
printf("\n");
display(hashtable[3]);
printf("\n");*/

if(n > 0 ){

    return n;
}

return 0;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{

unsigned int index = hash(word);
node *cursor = hashtable[index];

while(cursor != NULL){

    if (strcasecmp(word , cursor -> word) == 0)
    {
        return true;
    }

    cursor = cursor -> next;
}

return false;
}


{

for (int i = 0; i < N; i++)
{
    node *cursor = hashtable[i];

    while (cursor != NULL)
    {
        node *tmp = cursor;
        cursor = cursor->next;
        free(tmp);
    }

}

return true;
}
0

The first node for a given bin does not get installed in your hash table immediately. It does so indirectly if there's a second word for the same letter. The checker uses a small dictionary (check the numbers!), so this likely never happens.

HEAD and check both make no sense to me. I think in most instances you should use hashtable[index] instead of HEAD, also there's no need to test whether it's the first node for this bin.

1
  • Thank you, it works now perfectly – Ahmed Alaa Aug 30 '19 at 11:41

You must log in to answer this question.

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