Advertisement
Jodyone

dictionary.c

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