SHARE
TWEET

Untitled

a guest Apr 21st, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Implements a dictionary's functionality
  2.  
  3. #include <ctype.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <stdlib.h>
  9.  
  10. #include "dictionary.h"
  11.  
  12. // Represents number of buckets in a hash table
  13. #define N 26
  14.  
  15. // Represents a node in a hash table
  16. typedef struct node
  17. {
  18.     char word[LENGTH + 1];
  19.     struct node *next;
  20. }
  21. node;
  22.  
  23. // Represents a hash table
  24. node *hashtable[N];
  25. // word count
  26. int wordcount=0;
  27. bool loaded=false;
  28. // Hashes word to a number between 0 and 25, inclusive, based on its first letter
  29. unsigned int hash(const char *word)
  30. {
  31.     return tolower(word[0]) - 'a';
  32. }
  33. // Loads dictionary into memory, returning true if successful else false
  34. bool load(const char *dictionary)
  35. {
  36.     node *head;
  37.     // Initialize hash table
  38.     for (int i = 0; i < N; i++)
  39.     {
  40.         hashtable[i] = NULL;
  41.     }
  42.  
  43.     // Open dictionary
  44.     FILE *file = fopen(dictionary, "r");
  45.     if (file == NULL)
  46.     {
  47.         unload();
  48.         return false;
  49.     }
  50.  
  51.     // Buffer for a word
  52.     char word[LENGTH + 1];
  53.  
  54.     // Insert words into hash table
  55.     while (fscanf(file, "%s", word) != EOF)
  56.     {
  57.         int h=hash(word);
  58.         head=(node *)malloc(sizeof(node));
  59.         if(head==NULL)
  60.         {
  61.             unload();
  62.             return false;
  63.         }
  64.  
  65.         //  intially when hashtable[h] contains NULL
  66.         if(hashtable[h]==NULL)
  67.         {
  68.             strcpy(head->word,word);
  69.             hashtable[h]=head;
  70.             head->next=NULL;
  71.             wordcount++;
  72.         }
  73.  
  74.         // for adding a node to hashtable[h] when already first word is added
  75.         else
  76.         {
  77.             // head=(node *)malloc(sizeof(node));
  78.             strcpy(head->word,word);
  79.             head->next=hashtable[h];
  80.             hashtable[h]=head;
  81.             wordcount++;
  82.         }
  83.     }
  84.     // Close dictionary
  85.     free(head);
  86.     fclose(file);
  87.  
  88.     // Indicate success
  89.     loaded=true;
  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. {
  96.     if(loaded==true)
  97.     return wordcount;
  98.     return 0;
  99. }
  100.  
  101. // Returns true if word is in dictionary else false
  102. bool check(const char *word)
  103. {
  104.     node *cursor=hashtable[hash(word)];
  105.     while(cursor != NULL)
  106.     {
  107.         if(strcasecmp(cursor->word,word)==0)
  108.         {
  109.         return true;
  110.         }
  111.         else
  112.         cursor=cursor->next;
  113.     }
  114.     return false;
  115. }
  116.  
  117. // Unloads dictionary from memory, returning true if successful else false
  118. bool unload(void)
  119. {
  120.     for(int i=0 ; i < N ; i++)
  121.     {
  122.         node *cursor=hashtable[i];
  123.         while(cursor!=NULL)
  124.         {
  125.         node *temp=cursor;
  126.         cursor=cursor->next;
  127.         free(temp);
  128.         }
  129.     }
  130.     return true;
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top