Advertisement
Yonka2019

linkedList.c

Jun 19th, 2021
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.65 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. /*********************************
  3. * Class: MAGSHIMIM C2            *
  4. * Week: 11                       *
  5. * Name: Final Project            *
  6. * Credits: Yonka                 *
  7. **********************************/
  8.  
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #define _CRTDBG_MAP_ALLOC
  14. #include <crtdbg.h>
  15. #include "linkedList.h"
  16.  
  17. #define STR_LEN 50
  18. #define FALSE 0
  19. #define TRUE !FALSE
  20.  
  21. void changeIndex(FrameNode** head, char* name, int index);
  22. void changeDuration(FrameNode** head, char* name, int duration);
  23. void changeAllDurations(FrameNode** head, int duration);
  24. void freeMemory(FrameNode* head);
  25. void insertAtEnd(FrameNode** head, Frame* newNode);
  26. void printList(FrameNode* head);
  27. void myFgets(char str[], int n);
  28. int selectMenu();
  29. int getLength(FrameNode* head);
  30. bool frameExist(FrameNode* head, char* name);
  31. bool removeFrame(FrameNode** head, char* name);
  32. Frame* initFrame(FrameNode* head);
  33. FrameNode* createFrameNode();
  34. Frame* createFrame(char* path, int duration, char* name);
  35.  
  36. Frame* initFrame(FrameNode* head)
  37. {
  38.     char path[STR_LEN] = { 0 };
  39.     char name[STR_LEN] = { 0 };
  40.     int duration = 0;
  41.     bool alreadyFrameExist = FALSE;
  42.  
  43.     printf("*** Creating new Frame ***\n");
  44.  
  45.     printf("Please insert Frame path:\n");
  46.     myFgets(path, STR_LEN);
  47.  
  48.     printf("Please insert frame duration(in milliseconds):\n");
  49.     scanf("%d", &duration);
  50.     getchar();
  51.  
  52.     printf("Please choose a name for that frame:\n");
  53.     do
  54.     {
  55.         myFgets(name, STR_LEN);
  56.         alreadyFrameExist = frameExist(head, name);
  57.         if (alreadyFrameExist)
  58.         {
  59.             printf("The name is already taken, please enter another name\n");
  60.         }
  61.     } while (alreadyFrameExist);
  62.  
  63.     return createFrame(path, duration, name);
  64. }
  65. Frame* createFrame(char* path, int duration, char* name)
  66. {
  67.     Frame* newFrame = (Frame*)malloc(sizeof(Frame));
  68.     newFrame->path = (char*)malloc((strlen(path) + 1) * sizeof(char));
  69.     newFrame->name = (char*)malloc((strlen(name) + 1) * sizeof(char));
  70.  
  71.     strcpy(newFrame->path, path);
  72.     newFrame->duration = duration;
  73.     strcpy(newFrame->name, name);
  74.  
  75.     return newFrame;
  76. }
  77. FrameNode* createFrameNode()
  78. {
  79.     FrameNode* newFrameNode = (FrameNode*)malloc(sizeof(FrameNode));
  80.     if (!newFrameNode)
  81.     {
  82.         printf("Unsuccessful malloc!\n");
  83.         getchar();
  84.         exit(-1);
  85.     }
  86.  
  87.     newFrameNode->frame = NULL;
  88.     newFrameNode->next = NULL;
  89.  
  90.     return newFrameNode;
  91. }
  92. void myFgets(char* str, int n)
  93. {
  94.     fgets(str, n, stdin);
  95.     str[strcspn(str, "\n")] = 0;
  96. }
  97. bool frameExist(FrameNode* head, char* name)
  98. {
  99.     FrameNode* p = head;
  100.  
  101.     while (p)
  102.     {
  103.         if (strcmp(p->frame->name, name) == 0)
  104.         {
  105.             return TRUE;
  106.         }
  107.         p = p->next;
  108.     }
  109.     return FALSE;
  110. }
  111. void insertAtEnd(FrameNode** head, Frame* newFrame)
  112. {
  113.     if (!*head)
  114.     {
  115.         (*head) = createFrameNode();
  116.         (*head)->frame = newFrame;
  117.     }
  118.     else
  119.     {
  120.         FrameNode* p = *head;
  121.         while (p->next)
  122.         {
  123.             p = p->next;
  124.         }
  125.         p->next = createFrameNode();
  126.         p->next->frame = newFrame;
  127.     }
  128. }
  129. void printList(FrameNode* head)
  130. {
  131.     FrameNode* p = head;
  132.  
  133.     printf("\t\tName\t\tDuration\t\tPath\n");
  134.     while (p)
  135.     {
  136.         printf("\t\t%s", p->frame->name);
  137.         printf("\t\t%d ms", p->frame->duration);
  138.         printf("\t\t\t%s\n", p->frame->path);
  139.  
  140.         p = p->next;
  141.     }
  142.     printf("\n");
  143. }
  144. bool removeFrame(FrameNode** head, char* name)
  145. {
  146.     FrameNode* p = *head;
  147.     FrameNode* dNode = NULL;
  148.  
  149.     if (*head)
  150.     {
  151.         if (0 == strcmp((*head)->frame->name, name))
  152.         {
  153.             *head = (*head)->next;
  154.             free(p->frame->path);
  155.             free(p->frame->name);
  156.             free(p->frame);
  157.             free(p);
  158.  
  159.             return TRUE;
  160.         }
  161.         else
  162.         {
  163.             while (p->next &&
  164.                 0 != strcmp(p->next->frame->name, name))
  165.             {
  166.                 p = p->next;
  167.             }
  168.             if (p->next)
  169.             {
  170.                 dNode = p->next;
  171.                 p->next = dNode->next;
  172.                 free(dNode);
  173.  
  174.                 return TRUE;
  175.             }
  176.         }
  177.     }
  178.     return FALSE;
  179. }
  180. void changeIndex(FrameNode** head, char* name, int index)
  181. {
  182.     FrameNode* root = *head;
  183.     FrameNode* link = root;
  184.     FrameNode* prev = 0;
  185.  
  186.     int count = 0;
  187.     if (getLength(root) < index)
  188.     {
  189.         printf("The index is greater than the list size\n");
  190.         return;
  191.     }
  192.     index--;
  193.     while (link && strcmp(link->frame->name, name) != 0)
  194.     {
  195.         prev = link;
  196.         link = link->next;
  197.         count++;
  198.     }
  199.     if (!link)  // Name not found - no swap
  200.     {
  201.         printf("This frame doesn't exist!\n");
  202.         return;
  203.     }
  204.     if (count == index)   // Already in target position - no swap
  205.     {
  206.         printf("This frame already in this index\n");
  207.         return;
  208.     }
  209.     if (count == 0)     // Moving first item; update root
  210.     {
  211.         *head = root->next;
  212.         root = *head;
  213.     }
  214.     else
  215.     {
  216.         prev->next = link->next;
  217.     }
  218.     // link is detached; now where does it go?
  219.     if (index == 0)       // Move to start; update root
  220.     {
  221.         link->next = root;
  222.         *head = link;
  223.         return;
  224.     }
  225.     FrameNode* node = root;
  226.     for (int i = 0; i < index - 1 && node->next != 0; i++)
  227.     {
  228.         node = node->next;
  229.     }
  230.     link->next = node->next;
  231.     node->next = link;
  232. }
  233. int getLength(FrameNode* head)
  234. {
  235.     int count = 0;
  236.     FrameNode* current = head;
  237.  
  238.     while (current)
  239.     {
  240.         count++;
  241.         current = current->next;
  242.     }
  243.  
  244.     return count;
  245. }
  246. void changeDuration(FrameNode** head, char* name, int duration)
  247. {
  248.     FrameNode* p = *head;
  249.     while (p && strcmp(p->frame->name, name) != 0)
  250.     {
  251.         p = p->next;
  252.     }
  253.     if (!p)  // Name not found - no swap
  254.     {
  255.         printf("This frame doesn't exist!\n");
  256.         return;
  257.     }
  258.     p->frame->duration = duration;
  259. }
  260. void changeAllDurations(FrameNode** head, int duration)
  261. {
  262.     FrameNode* p = *head;
  263.     while (p)
  264.     {
  265.         p->frame->duration = duration;
  266.         p = p->next;
  267.     }
  268. }
  269. void freeMemory(FrameNode* head)
  270. {
  271.     FrameNode* temp = head;
  272.     while (head)
  273.     {
  274.         temp = head->next;
  275.         free(head->frame->path);
  276.         free(head->frame->name);
  277.         free(head->frame);
  278.         free(head);
  279.         head = temp;
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement