Ryba3310

hash table

Apr 26th, 2022
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <stdbool.h>
  6. #include <stdlib.h>
  7. #include <strings.h>
  8.  
  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. // TODO: Choose number of buckets in hash table
  19. const unsigned int N = 80;
  20.  
  21. // Hash table
  22. node *table[N];
  23.  
  24. void free_list(struct node *n)
  25. {
  26.     while(n != NULL)
  27.     {
  28.         node *tmp = n->next;
  29.         free(n);
  30.         n = tmp;
  31.     }
  32. }
  33.  
  34. bool insert_node(struct node *w)
  35. {
  36.     unsigned int table_index = hash(w->word);
  37.     if (table[table_index] == NULL)
  38.     {
  39.         table[table_index] = w;
  40.         return true;
  41.     }
  42.     for (node *n = table[table_index]; n != NULL; n = n->next)
  43.     {
  44.         if (n->next == NULL)
  45.         {
  46.             n->next = w;
  47.             return true;
  48.         }
  49.     }
  50.     return false;
  51. }
  52.  
  53. // Returns true if word is in dictionary, else false
  54. bool check(const char *word)
  55. {
  56.     // TODO
  57.     unsigned int word_hash = hash(word);
  58.     for (node *n = table[word_hash]; n != NULL; n = n->next)
  59.     {
  60.         if (strcasecmp(n->word, word))
  61.         {
  62.             return true;
  63.         }
  64.     }
  65.     return false;
  66. }
  67.  
  68. // Hashes word to a number
  69. unsigned int hash(const char *word)
  70. {
  71.     // TODO: Improve this hash function
  72.     short ind = 0;
  73.     unsigned int hash_val = 0;
  74.     while (word[ind] != '\0')
  75.     {
  76.         if (isalpha(word[ind]))
  77.         {
  78.             hash_val += word[ind];
  79.         }
  80.         ind++;
  81.     }
  82.     return hash_val % N;
  83.     // return tolower(word[0]) * tolower(word[1]) % N;
  84. }
  85.  
  86. // Loads dictionary into memory, returning true if successful, else false
  87. bool load(const char *dictionary)
  88. {
  89.     // TODO
  90.     FILE *dict = fopen(dictionary, "r");
  91.     if (dict == NULL)
  92.     {
  93.         printf("Could not open a dictionary file.\n");
  94.         fclose(dict);
  95.         return false;
  96.     }
  97.  
  98.     node *word = malloc(sizeof(struct node));
  99.     if (word != NULL)
  100.     {
  101.         printf("Starting loading dictionary\n");
  102.         int char_index = 0;
  103.         char c;
  104.         while (fread(&c, sizeof(char), 1, dict))
  105.         {
  106.             if (isalpha(c) || (c == '\'' && char_index > 0))
  107.             {
  108.                 word->word[char_index] = c;
  109.                 char_index++;
  110.             }
  111.             else if (c == '\n')
  112.             {
  113.                 word->word[char_index + 1] = '\0';
  114.                 if (insert_node(word))
  115.                 {
  116.                     node *next_word = malloc(sizeof(struct node));
  117.                     if (next_word == NULL)
  118.                     {
  119.                         printf("Could not load next word.\n");
  120.                         fclose(dict);
  121.                         return false;
  122.                     }
  123.                     word->next = next_word;
  124.                     word = next_word;
  125.                     char_index = 0;
  126.                 }
  127.             }
  128.         }
  129.         printf("LOADED!\n");
  130.         fclose(dict);
  131.         return true;
  132.     }
  133.     printf("Couldn't load dictionary\n");
  134.     fclose(dict);
  135.     return false;
  136. }
  137.  
  138. // Returns number of words in dictionary if loaded, else 0 if not yet loaded
  139. unsigned int size(void)
  140. {
  141.     // TODO
  142.     printf("checking size\n");
  143.     unsigned int elements = 0;
  144.     for (int i = 0; i < N; i++)
  145.     {
  146.         for (node *j = table[i]; j != NULL; j = j->next)
  147.         {
  148.             elements++;
  149.         }
  150.     }
  151.     printf("Returning size\n");
  152.     return elements;
  153. }
  154.  
  155. // Unloads dictionary from memory, returning true if successful, else false
  156. bool unload(void)
  157. {
  158.     // TODO
  159.     for (int i = 0; i < N; i++)
  160.     {
  161.         if (table[i] != NULL)
  162.         {
  163.             free_list(table[i]);
  164.         }
  165.     }
  166.     return true;
  167. }
  168.  
  169. /* void free_list(struct node *n)
  170. {
  171.     if(n == NULL)
  172.     {
  173.         return;
  174.     }
  175.     free_list(n->next);
  176.     free(n);
  177.     return;
  178. } */
  179. /* void insert_node(struct node *w)
  180. {
  181.     unsigned int table_index = hash(w->word);
  182.     if (table[table_index] == NULL)
  183.     {
  184.         table[table_index] = w;
  185.         return;
  186.     }
  187.     for (node *n = table[table_index]; n != NULL; n = n->next)
  188.     {
  189.         if (n->next == NULL)
  190.         {
  191.             n->next = w;
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment