Advertisement
Jodyone

dictionary.c

Oct 23rd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.89 KB | None | 0 0
  1. /****************************************************************************
  2.  * dictionary.c
  3.  *
  4.  * Computer Science 50
  5.  * Problem Set 5
  6.  *
  7.  * Implements a dictionary's functionality.
  8.  ***************************************************************************/
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include "dictionary.h"
  13. #include <ctype.h>
  14. #include <string.h>
  15.  
  16.  
  17. #define SIZE 250000
  18.  
  19. typedef struct node
  20. {
  21.     char word[LENGTH + 1];  
  22.     struct node* next;
  23. }
  24. node;
  25.  
  26. // Make a hash table
  27. node* hashtable[SIZE];
  28. char temp_word[LENGTH + 1];
  29. unsigned int word_ctr = 0;
  30.  
  31. // this hash function is from delipity on reddit
  32. int hash_d(char* word)
  33. {
  34.    
  35.     unsigned int hash = 0;
  36.     for (int i = 0, n = strlen(word); i < n; i++)
  37.     {
  38.         hash = (hash << 2) ^ word[i];
  39.     }
  40.      
  41.     return hash % SIZE;
  42. }
  43. /**
  44.  * Returns true if word is in dictionary else false.
  45.  */
  46. bool check(const char* word)
  47. {
  48.     // TODO
  49.     char new_word[LENGTH + 1];
  50.     for (int i = 0, n = strlen(word); i <= n; i++)
  51.     {
  52.         new_word[i] = word[i];
  53.         new_word[i] = tolower(new_word[i]);
  54.     }
  55.      
  56.     // determine the correct hash position
  57.     int y = hash_d(new_word);
  58.  
  59.     // point the cursor to the first item in the list we identified
  60.     node* cursor = hashtable[y];
  61.    
  62.     // search the linked list
  63.     while (cursor != NULL)
  64.     {
  65.         if (strcmp1(cursor->word,new_word) == 0)
  66.         {  
  67.             return true;
  68.         }
  69.         else
  70.         {
  71.             cursor = cursor->next;
  72.         }
  73.     }
  74.     return false;
  75. }
  76.  
  77.  
  78. /**
  79.  * Loads dictionary into memory.  Returns true if successful else false.
  80.  */  
  81. bool load(const char* dictionary)
  82. {
  83.     // TODO
  84.     // do some prep work
  85.     FILE* inptr = fopen(dictionary,"r");
  86.     if (inptr == NULL)
  87.     {
  88.         printf("Could not open %s.\n",dictionary);
  89.         return 2;
  90.     }
  91.    
  92.      // while there are words available
  93.     while (fscanf(inptr,"%s\n",temp_word) != EOF)
  94.     {
  95.         node* new_node = malloc(sizeof(node));
  96.         new_node->next = NULL;
  97.         strcpy(new_node->word,temp_word);
  98.         word_ctr++;
  99.         int x = hash_d(new_node->word);
  100.         // get it into that list
  101.         new_node->next = hashtable[x];
  102.         hashtable[x] = new_node;  
  103.     }
  104.     // do some cleanup work
  105.     fclose(inptr);
  106.     return true;
  107.  }
  108. /**
  109.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  110.  */
  111. unsigned int size(void)
  112. {
  113.     // TODO
  114.     return word_ctr;
  115. }
  116.  
  117. /**
  118.  * Unloads dictionary from memory.  Returns true if successful else false.
  119.  */
  120. bool unload(void)
  121. {
  122.     // TODO
  123.  
  124.     for (int i = 0; i < SIZE ; i++)
  125.     {
  126.         node* cursor = hashtable[i];
  127.         while (cursor != NULL)
  128.         {
  129.             node* temp = cursor;
  130.             cursor = cursor->next;
  131.             free(temp);
  132.         }
  133.     }
  134.     return true;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement