Advertisement
Yonka2019

linkedList.c

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