Advertisement
Guest User

Untitled

a guest
Nov 10th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.90 KB | None | 0 0
  1. // Implements a dictionary's functionality
  2.  
  3. #include <stdbool.h>
  4.  
  5. #include "dictionary.h"
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <strings.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12. #include <cs50.h>
  13. #include <ctype.h>
  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. // Number of buckets in hash table
  24. const unsigned int N = 1;
  25.  
  26. // Hash table
  27. node *table[N];
  28.  
  29. // Global Variable for word count incremented in load() called and returned in size()
  30. int nWordCount = 0;
  31.  
  32. // Returns true if word is in dictionary else false
  33. bool check(const char *word)
  34. {
  35.    
  36.     // TODO
  37.    
  38.     bool bFound = false;
  39.    
  40.     int key = hash(word);
  41.    
  42.     node *cursor = malloc(sizeof(node));
  43.    
  44.     if (cursor == NULL)
  45.     {
  46.         printf("\n \t Unable to malloc check()");
  47.         return 1;
  48.     }
  49.    
  50.     //checks if the list is outright empty : meaning the hash did not map correctly
  51.     if (table[key]->next == NULL)
  52.     {
  53.         printf("\n \t Dictionary is empty");
  54.         return 1;
  55.     }
  56.    
  57.     //if list is not empty
  58.     else
  59.     {
  60.         for(cursor = table[key]; cursor->next != NULL; cursor=cursor->next)
  61.         {
  62.             if (strcasecmp(word, cursor->word))
  63.                 bFound = true;
  64.         }
  65.     }
  66.    
  67.     free(cursor);
  68.     return bFound;
  69. }
  70.  
  71. // Hashes word to a number
  72. unsigned int hash(const char *word)
  73. {
  74.     // TODO
  75.     return 0;
  76. }
  77.  
  78. // Loads dictionary into memory, returning true if successful else false
  79. bool load(const char *dictionary)
  80. {
  81.     // TODO
  82.  
  83.  
  84.     int key = 0;
  85.     char word[LENGTH];
  86.     bool bState = true;
  87.  
  88.    
  89.     //initialize values for hash table. word = "" and next = NULL. no dangling pointers
  90.     for(int i = 0; i < N; i++)
  91.     {
  92.         table[i] = NULL;
  93.         //strcpy(table[i]->word, "\0");
  94.         //table[i]->next = NULL;
  95.     }
  96.    
  97.  
  98.     //node cursor to mark head node of linked list
  99.     node *cursor = malloc(sizeof(node));
  100.  
  101.     //check if able to malloc cursor
  102.     if(cursor == NULL)
  103.     {
  104.         printf("\n \t Unable to malloc cursor in load()");
  105.         bState = false;
  106.         return 1;
  107.     }
  108.  
  109.     //initialize values in node cursor
  110.     strcpy(cursor->word, "");
  111.     cursor->next = NULL;
  112.    
  113.  
  114.     //open file
  115.     FILE *fp1 = fopen(dictionary, "r");
  116.  
  117.     //check that the file was opened
  118.     if (fp1 == NULL)
  119.     {
  120.         printf("\n \t Error opening file");
  121.         bState = false;
  122.         return 1;
  123.     }
  124.  
  125.     //reading each word in the file, while not EOF
  126.     while (fscanf(fp1, "%s", word) != EOF)
  127.     {
  128.         //creating the node n which will hold the word form the dictionary file
  129.         node *n = malloc(sizeof(node));
  130.  
  131.         //check if node was created
  132.         if (n == NULL)
  133.         {
  134.             printf("\n \t Unable to malloc node n in load()");
  135.             bState = false;
  136.             return 1;
  137.         }
  138.  
  139.         //initialize values in node n
  140.         strcpy(n->word, word);
  141.         n->next = NULL;
  142.  
  143.         //counter for total words loaded
  144.         nWordCount++;
  145.  
  146.         //call hash function to get value of word
  147.         key = hash(word);
  148.  
  149.         //insert first node / head if array is empty
  150.         if (table[key] == NULL)
  151.         {
  152.             table[key] = n;
  153.         }
  154.  
  155.         //else array is not empty, insert new node at the end
  156.         else if (table[key] != NULL)
  157.         {
  158.             cursor = table[key];
  159.             n->next = table[key]->next;
  160.             cursor->next = n;
  161.         }
  162.     }
  163.    
  164.     free(cursor);
  165.    
  166.     fclose(fp1);
  167.  
  168.     return bState;
  169. }
  170.  
  171. // Returns number of words in dictionary if loaded else 0 if not yet loaded
  172. unsigned int size(void)
  173. {
  174.     // TODO
  175.     //return 0;
  176.  
  177.     return nWordCount;
  178. }
  179.  
  180. // Unloads dictionary from memory, returning true if successful else false
  181. bool unload(void)
  182. {
  183.     // TODO
  184.    
  185.     int i;
  186.     int j;
  187.     bool bState = false;
  188.  
  189.     //node runner + trailer to traverse link lists
  190.     node *runner = malloc(sizeof(node));
  191.     node *trailer = malloc(sizeof(node));
  192.  
  193.     //check if able to malloc cursor
  194.     if(runner == NULL)
  195.     {
  196.         printf("\n \t Unable to malloc runner in load()");
  197.         bState = false;
  198.         return 1;
  199.     }
  200.    
  201.     //check if able to malloc cursor
  202.     if(trailer == NULL)
  203.     {
  204.         printf("\n \t Unable to malloc trailer in load()");
  205.         bState = false;
  206.         return 1;
  207.     }
  208.  
  209.     //initialize values in runner
  210.     strcpy(runner->word, "");
  211.     runner->next = NULL;
  212.  
  213.     //initialize values in trailer
  214.     strcpy(trailer->word, "");
  215.     trailer->next = NULL;
  216.  
  217.  
  218.     //to traverse each linked list in each array
  219.     for (i = 0; i < N; i++)
  220.     {
  221.         //if index is outright NULL, increment and go to the next slot
  222.         if(table[i]->next == NULL)
  223.             i++;
  224.        
  225.         //set runner + trailer to index
  226.         runner = table[i];
  227.         trailer = table[i];
  228.        
  229.        
  230.         do
  231.         {
  232.             //move trailer = to runner
  233.             trailer = runner;
  234.            
  235.             //means only 1 node on the list
  236.             if(runner->next == NULL)
  237.                 free(runner);
  238.        
  239.             //move runner 1 node forward (now 1 node ahead of trailer)        
  240.             else
  241.             {
  242.                 runner = runner->next;
  243.                 free(trailer);
  244.             }
  245.        
  246.         }while(runner->next != NULL);
  247.        
  248.         //assumption is that all linked lists have been free()'d after the for loop
  249.         //all that is left is the array table[i] itself
  250.        
  251.         for(j = 0; j < N; j++)
  252.         {
  253.             strcpy(trailer->word, "");
  254.             trailer->next = NULL;
  255.         }    
  256.    
  257.     free(trailer);    
  258.     free(runner);
  259.     }
  260.    
  261.     //quick check to see it was done for all array slots
  262.     if((i == N) && (j == N))
  263.         bState = true;
  264.  
  265.  
  266.     return bState;
  267. }
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement