Guest User

dictionary.c

a guest
May 1st, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.84 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdbool.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include<strings.h>
  9. #include "dictionary.h"
  10.  
  11. // Represents a node in a hash table
  12. typedef struct node
  13. {
  14.     char word[LENGTH + 1];
  15.     struct node *next;
  16. } node;
  17.  
  18. //function to delete the linked list
  19. void deleteList(node *head)
  20. {
  21.     if (head == NULL)
  22.     {
  23.         return;
  24.     }
  25.     deleteList(head->next);
  26.     free(head);
  27. }
  28.  
  29. // Number of buckets in hash table
  30. const unsigned int N = 65536;
  31.  
  32. // Initialise Hash table
  33. node *table[N]; //basically initializes an array of size N that stores address to a node of a linked list (Since it's a pointer)
  34.  
  35. //variable to keep track of the size (i.e. the number of words in dictionary)
  36. int noOfWords = 0; //global variable
  37.  
  38. // Hashes word to a number
  39. // Reference Link: https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/cf9nlkn/
  40. unsigned int hash(const char *word)
  41. {
  42.     unsigned int hash = 0;
  43.     for (int i = 0, n = strlen(word); i < n; i++)
  44.         hash = (hash << 2) ^ word[i];
  45.     return hash % N;
  46.     return 0;
  47. }
  48. // Produces a total of 2^16 buckets
  49.  
  50. // Loads dictionary into memory, returning true if successful else false
  51. bool load(const char *dictionary)
  52. {
  53.     //open dictionary using fopen function that takes 2 arguments; string name of the file to be opened and permissions
  54.     FILE *dictPtr = fopen(dictionary, "r");
  55.     if (dictPtr == NULL)
  56.     {
  57.         return false;
  58.     }
  59.  
  60.     //initialise a char array to store the words read from the dictionary one by one
  61.     char word[LENGTH + 1];
  62.  
  63.     //loop to read every word from our dictionary until we reach the end of the file and insert each word into our hash table thus loading it
  64.     while (fscanf(dictPtr, "%s", word) != EOF)
  65.     {
  66.         //making a new node and allocating it memory
  67.         node *temp = malloc(sizeof(node));
  68.  
  69.  
  70.         //check if the malloc doesn't return NULL
  71.         if (temp == NULL)
  72.         {
  73.             return false;
  74.         }
  75.         else
  76.         {
  77.             //using string copy function to copy the word from array word to our node
  78.             strcpy(temp->word, word);
  79.             noOfWords++;
  80.             //determining the index using hash function
  81.             int index = hash(temp->word);
  82.             //inserting the node at the specified index
  83.             temp->next = table[index];
  84.             table[index] = temp;
  85.         }
  86.     }
  87.     //close the dictionary file
  88.     fclose(dictPtr);
  89.  
  90.     return true;
  91. }
  92.  
  93. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  94. unsigned int size(void) {
  95.     return noOfWords;
  96. }
  97.  
  98.  
  99. // Returns true if word is in dictionary else false
  100. bool check(const char *word)
  101. {
  102.     char lowerCaseWord[LENGTH + 1];
  103.  
  104.     //stores the lower case word (to remove the case sensitivity issue) in another array
  105.     for (int i = 0; i < LENGTH; i++)
  106.     {
  107.         lowerCaseWord[i] = tolower(word[i]);
  108.     }
  109.  
  110.     //passing the word into the hash function
  111.     int index = hash(lowerCaseWord);
  112.  
  113.     //make a temporary pointer to the nodes say cursor and point it to the first node of the linked list at that bucket item
  114.     // transverse through that linked list
  115.     for (node *cursor = table[index]; cursor != NULL; cursor = cursor->next)
  116.     {
  117.         //compare the words with those in the list; if matches return true
  118.         if (strcasecmp(word, cursor->word) == 0) //since this function return 1 if both the strings are equal
  119.         {
  120.             return true;
  121.         }
  122.     }
  123.     return false;
  124. }
  125.  
  126. // Unloads dictionary from memory, returning true if successful else false
  127. bool unload(void)
  128. {
  129.     for (int i = 0; i < N; i++)
  130.     {
  131.         //call a recursive function to delete the linked list at each bucket
  132.         deleteList(table[i]);
  133.     }
  134.     return true;
  135. }
Add Comment
Please, Sign In to add comment