Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.63 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <cs50.h>
  9.  
  10.  
  11. #include "dictionary.h"
  12.  
  13. // Represents a node in a hash table
  14. typedef struct node
  15. {
  16.     char word[LENGTH + 1];
  17.     struct node *next;
  18. }
  19. node;
  20.  
  21. int number_of_words = 0;
  22.  
  23. // Number of buckets in hash table
  24. const unsigned int N = 20000;
  25.  
  26. // Hash table
  27. node *table[N];
  28.  
  29. // Returns true if word is in dictionary else false
  30. bool check(const char *word)
  31. {
  32.     int hash_num = hash(word);
  33.  
  34.     node *p = table[hash_num];
  35.     while (p != NULL)
  36.     {
  37.         int a = strcasecmp(p->word, word);
  38.         if (a == 0)
  39.         {
  40.             return true;
  41.         }
  42.         p = p->next;
  43.     }
  44.  
  45.     return false;
  46. }
  47.  
  48. // Hashes word to a number
  49. unsigned int hash(const char *word)
  50. {
  51.     int sum = 0;
  52.     while(*word != '\0')
  53.     {
  54.  
  55.         if(*word >='A' && *word < 'A'+26)
  56.              sum=sum+(*word -'A' + 1);
  57.         else if(*word >='a' && *word < 'a'+26)
  58.              sum=sum+(*word -'a' + 1);
  59.         else
  60.              return -1;
  61.  
  62.         word++;
  63.     }
  64.  
  65.     return sum;
  66. }
  67.  
  68. // Loads dictionary into memory, returning true if successful else false
  69. bool load(const char *dictionary)
  70. {
  71.     // Openning the dictionary file for reading
  72.     FILE *file = fopen(dictionary, "r");
  73.     if ( fopen(dictionary, "r") == NULL)
  74.     {
  75.         return false;
  76.     }
  77.  
  78.     string word = NULL;
  79.     while (fscanf(file, "%s", word) != EOF)
  80.     {
  81.         node *n = malloc(sizeof(node));
  82.         if (n == NULL)
  83.         {
  84.             return 1;
  85.             return false;
  86.         }
  87.         n->next = NULL;
  88.         strcpy(n->word, word);
  89.  
  90.         //Handling collision
  91.         if (table[hash(word)] != NULL)
  92.         {
  93.             n->next = table[hash(word)];
  94.  
  95.             table[hash(word)] = n;
  96.         }
  97.         else
  98.         {
  99.             table[hash(word)] = n;
  100.         }
  101.  
  102.     }
  103.     return true;
  104. }
  105.  
  106. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  107. unsigned int size(void)
  108. {
  109.     return number_of_words;
  110. }
  111.  
  112. // Unloads dictionary from memory, returning true if successful else false
  113. bool unload(void)
  114. {
  115.     node *cursor = malloc(sizeof(node));
  116.     if (cursor == NULL)
  117.     {
  118.         return 1;
  119.         return false;
  120.     }
  121.  
  122.     node *tmp = malloc(sizeof(node));
  123.     if (tmp == NULL)
  124.     {
  125.         return 1;
  126.         return false;
  127.     }
  128.  
  129.     int n = 0;
  130.     while (table[n] != NULL)
  131.     {
  132.         cursor = table[n]->next;
  133.  
  134.         tmp = table[n];
  135.  
  136.         free(tmp);
  137.  
  138.         tmp = cursor;
  139.     }
  140.  
  141.     return false;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement