Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Linked List Array Or Trie?

a guest Nov 17th, 2018 88 in 1 day
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cs50.h>
  4. #include <math.h>
  5. #include <string.h>
  6.  
  7. //Node type structure for a character
  8. typedef struct character
  9. {
  10. char ch;
  11. struct character* next;
  12. struct character* nextWord;
  13. }
  14. character;
  15.  
  16. //Linked list
  17. character* linkedList = NULL;
  18.  
  19. int wordCount = 0;
  20.  
  21. int main(void)
  22. {
  23.     //Pointer to read a file
  24.     FILE * readPtr = fopen("small","r");
  25.     char ch;
  26.     //Checks if the File pointer is pointing to an existing file "small"
  27.     if (readPtr)
  28.     {
  29.         bool isNextWord = false;
  30.         //Check if it is at the end of the file, otherwise read entire file into ch
  31.         while ((ch = fgetc(readPtr)) != EOF)
  32.         {
  33.             //Define a character pointer that points to allocated memory the size of a character struct node
  34.             character* ptrToAllocatedChar = malloc(sizeof(character));
  35.             //If the computer ran out of memory, free the allocated memory and end the program (Safety Check)
  36.             if (ptrToAllocatedChar == NULL)
  37.             {
  38.                 free(ptrToAllocatedChar);
  39.                 return 1;
  40.             }
  41.             //store ch into character node which is allocated
  42.             ptrToAllocatedChar->ch = ch;
  43.             ptrToAllocatedChar->next = NULL;
  44.             if (linkedList)
  45.             {
  46.                 for (character* ptr = linkedList; ptr != NULL; ptr = ptr->next)
  47.                 {
  48.                     if (ptrToAllocatedChar->ch == '\n')
  49.                     {
  50.                         //Volgende woord implementasie
  51.                         free(ptrToAllocatedChar);
  52.                         isNextWord = true;
  53.                         ++wordCount;
  54.                         //Allocate memory for next word
  55.                         ptrToAllocatedChar->nextWord = malloc(sizeof(character));
  56.                         printf("\n");
  57.                         break;
  58.                     }
  59.                     if (wordCount == 0)
  60.                     {
  61.                         if (ptr->next == NULL)
  62.                         {
  63.                             ptr->next = ptrToAllocatedChar;
  64.                             printf("%c", ptr->next->ch);
  65.                             break;
  66.                         }
  67.                     }
  68.                     for (int i = 0; i < wordCount; i++)
  69.                     {
  70.                         if (ptr->nextWord == NULL)
  71.                         {
  72.                             ptr = ptr->nextWord;
  73.                         }
  74.                     }
  75.                     if (ptr->next == NULL)
  76.                     {
  77.                         ptr->next = ptrToAllocatedChar;
  78.                         break;
  79.                     }
  80.                 }
  81.             }
  82.             else
  83.             {
  84.                 linkedList = ptrToAllocatedChar;
  85.                 printf("%c", linkedList->ch);
  86.             }
  87.         }
  88.     }
  89.     else
  90.     {
  91.         return 1;
  92.     }
  93.     //Free memory
  94.     character* ptr = linkedList;
  95.     while (ptr != NULL)
  96.     {
  97.         character* next = ptr->next;
  98.         free(ptr);
  99.         ptr = next;
  100.     }
  101.     fclose(readPtr);
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top