Advertisement
krishkhubchand

fml pointers

Aug 2nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <cs50.h>
  5. #include <ctype.h>
  6.  
  7.  
  8. #define LENGTH 45
  9.  
  10. typedef struct linkedList {
  11.     char word[LENGTH + 1];
  12.     struct linkedList *next;
  13. }
  14. list;
  15.  
  16.  
  17.  
  18.  
  19. struct linkedList* create (string newWord) {  //for creating a newElement that can enter our dictionary hash table.
  20.     eprintf("create has been called successfully!\n");
  21.     list *newElement = malloc(sizeof(newWord));
  22.     if (newElement == NULL) { //check for failure
  23.         printf("fail \n");
  24.     }
  25.     eprintf("no problems w/creating space for the newElement!\n");
  26.     strcpy(newElement->word, newWord);
  27.     newElement->next = NULL; //nextPointer is null for safe keeping!
  28.     eprintf("newWord should have been assigned, as well as the element! I hope?\n");
  29.     return newElement;
  30. }
  31.  
  32. int hash (string newWord) {
  33.     int l = strlen(newWord);
  34.     for(int i = 0; i < l; i++) {
  35.         if (isalpha(newWord[i]) == 0) {
  36.             eprintf("fail!\n");
  37.             return -1;
  38.         }
  39.     }
  40.     int index = newWord[0] - 97;
  41.     return index;
  42. }
  43.  
  44. void insert (struct linkedList *newElement, struct linkedList *arrayAreaHeader) {
  45.     if (arrayAreaHeader == NULL) {
  46.         arrayAreaHeader = malloc(sizeof(list));
  47.         arrayAreaHeader = newElement;
  48.         eprintf("your new element has become the first array \n");
  49.     } else {
  50.         // check if NEXT is free, recursively; how do I do that? -> then I can assign the pointer value of Element to NEXT! ALT: add as head and make changes... faster as well!
  51.         for (list *temp = arrayAreaHeader; temp != NULL; temp = temp->next) {
  52.             if (temp->next == NULL) {
  53.                 temp->next = newElement;                 break;
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59. int main () {
  60.     // TODO
  61.     list *myDict[26] = { NULL };
  62.    FILE *file = fopen("small", "r");
  63.     if (file == NULL)
  64.     {
  65.     fprintf(stderr, "ah darn");
  66.     }
  67.  
  68.     char tempWord[30];
  69.  
  70.     fscanf(file, "%s", tempWord);
  71.     eprintf("%s first\n", tempWord);
  72.    
  73.  
  74.    
  75.     list *newElement = create(tempWord);
  76.     eprintf("%s\n", newElement->word);
  77.    
  78.    int numero = hash(tempWord);
  79.  
  80.     eprintf("do we get this far? \n");
  81.     insert(newElement, myDict[numero]);
  82.     eprintf("yo we made it: %s\n", myDict[numero]->word);
  83.   fclose(file);
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement