Guest User

Untitled

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