Advertisement
Yonka2019

question3.c

Jun 1st, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.42 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. #define STR_MAX_LEN 20
  9. #define FRIENDS_NUMBER 3
  10.  
  11. typedef struct personNode {
  12.     char name[STR_MAX_LEN];
  13.     int age;
  14.     bool vip;
  15.     struct personNode* next;
  16. } personNode;
  17.  
  18. void freeLine(personNode* head);
  19. void search(personNode* head);
  20. void printLine(personNode* head);
  21. void reverse(personNode** head_ref);
  22. void removeFromLine(personNode** head);
  23. void insertAtBegin(personNode** head, personNode* newNode);
  24. void israeliInsertToLine(personNode** head, char names[][STR_MAX_LEN], personNode* newNode);
  25. int printSelectMenu();
  26. int lineLength(personNode* head);
  27. personNode* addToLine(personNode* head);
  28. personNode* addVIPToLine(personNode* head);
  29.  
  30.  
  31. int main()
  32. {
  33.     int choice = 0;
  34.     personNode* head = NULL;
  35.  
  36.     while (1)
  37.     {
  38.         choice = printSelectMenu();
  39.  
  40.         switch (choice)
  41.         {
  42.             case 1:
  43.                 printLine(head);
  44.                 break;
  45.             case 2:
  46.                 head = addToLine(head);
  47.                 break;
  48.             case 3:
  49.                 removeFromLine(&head);
  50.                 break;
  51.             case 4:
  52.                 head = addVIPToLine(head);
  53.                 break;
  54.             case 5:
  55.                 search(head);
  56.                 break;
  57.             case 6:
  58.                 reverse(&head);
  59.                 break;
  60.             case 7:
  61.                 printf("Goodbye!");
  62.                 freeLine(head);
  63.                 getchar();
  64.                 return 0;
  65.         }
  66.     }
  67.  
  68.     freeLine(head);
  69.     getchar();
  70.     return 0;
  71. }
  72. /*
  73. * Prints the menu and returns the user selection
  74. * input: -
  75. * output: user selection
  76. */
  77. int printSelectMenu()
  78. {
  79.     int choice = 0;
  80.  
  81.     printf("Welcome to MagshiParty Line Management Software!\n"
  82.            "Please enter your choice from the following options :\n"
  83.            "1 - Print line\n"
  84.            "2 - Add person to line\n"
  85.            "3 - Remove person from line\n"
  86.            "4 - VIP guest\n"
  87.            "5 - Search in line\n"
  88.            "6 - Reverse line\n"
  89.            "7 - Exit\n");
  90.     scanf("%d", &choice);
  91.     getchar();
  92.  
  93.     return choice;
  94. }
  95. /* Prints the line
  96. * input: head of the line
  97. * output: -
  98. */
  99. void printLine(personNode* head)
  100. {
  101.     personNode* current_node = head;
  102.  
  103.     printf("%d people in line:\n", lineLength(head));
  104.  
  105.     while (current_node != NULL)
  106.     {
  107.         printf("Name: %s, Age: %d\n", current_node->name, current_node->age);
  108.         current_node = current_node->next;
  109.     }
  110. }
  111. /* Counts the length of the line
  112. * input: the head of the line
  113. * output: the length of the line
  114. */
  115. int lineLength(personNode* head)
  116. {
  117.     if (!head)  // if list empty
  118.     {
  119.         return 0;
  120.     }
  121.     else
  122.     {
  123.         return 1 + lineLength(head->next);
  124.     }
  125. }
  126. /* Inserts new node into the end of the main node
  127. * input: head of the main node, new node
  128. * output: -
  129. */
  130. void insertAtEnd(personNode** head, personNode* newNode)
  131. {
  132.     if (!*head)
  133.     {
  134.         *head = newNode;
  135.     }
  136.     else
  137.     {
  138.         personNode* p = *head;
  139.         while (p->next)
  140.         {
  141.             p = p->next;
  142.         }
  143.         p->next = newNode;
  144.     }
  145. }
  146. /* Creates new node (type of - person)
  147. * input: -
  148. * output: -
  149. */
  150. personNode* createPersonNode()
  151. {
  152.     personNode* newPersonNode = (personNode*)malloc(sizeof(personNode));
  153.  
  154.     strcpy(newPersonNode->name, "No name");
  155.     newPersonNode->age = 0;
  156.     newPersonNode->vip = false;
  157.     newPersonNode->next = NULL;
  158.  
  159.     return newPersonNode;
  160. }
  161. /* Custom fgets() with built-in '\n' remove
  162. * input: string to input, string size
  163. * output: -
  164. */
  165. void myFgets(char str[], int n)
  166. {
  167.     fgets(str, n, stdin);
  168.     str[strcspn(str, "\n")] = 0;
  169. }
  170. /* asks user to input guest to add him into the line
  171. * input: head of the line
  172. * output: new head of the line
  173. */
  174. personNode* addToLine(personNode* head)
  175. {
  176.     int i = 0;
  177.     personNode* newNode = NULL;
  178.     char friends[FRIENDS_NUMBER][STR_MAX_LEN] = { 0 };
  179.  
  180.     printf("Welcome guest!\n");
  181.  
  182.     newNode = createPersonNode();
  183.  
  184.     printf("Enter name: ");
  185.     myFgets(newNode->name, STR_MAX_LEN);
  186.  
  187.     printf("Enter age: ");
  188.     scanf("%d", &(newNode->age));
  189.     getchar();
  190.  
  191.     printf("Enter name of 3 friends:\n");
  192.  
  193.     for (i = 0; i < FRIENDS_NUMBER; i++)
  194.     {
  195.         myFgets(friends[i], STR_MAX_LEN);
  196.     }
  197.    
  198.     israeliInsertToLine(&head, friends, newNode);
  199.  
  200.     return head;
  201. }  
  202. /* Inserts new node into the main node "Israeli line type"
  203. * input: head of the main node, names of the friends (2d-array), new node
  204. * output: -
  205. */
  206. void israeliInsertToLine(personNode** head, char names[][STR_MAX_LEN], personNode* newNode)
  207. {
  208.     if (*head)
  209.     {
  210.         personNode* p = *head;
  211.         if (0 == strcmp((*head)->name, names[0]) || 0 == strcmp((*head)->name, names[1]) || 0 == strcmp((*head)->name, names[2]))
  212.         {
  213.             newNode->next = (*head)->next;
  214.             (*head)->next = newNode;
  215.             return;
  216.         }
  217.         else
  218.         {
  219.             while (p->next &&
  220.                 (0 != strcmp(p->next->name, names[0]) || 0 != strcmp(p->next->name, names[1]) || 0 != strcmp(p->next->name, names[2])))
  221.             {
  222.                 p = p->next;
  223.             }
  224.             if (p->next)
  225.             {
  226.                 newNode->next = p->next;
  227.                 p->next = newNode;
  228.                 return;
  229.             }
  230.         }
  231.     }
  232.     insertAtEnd(head, newNode);
  233. }
  234. /* asks user to input guest to remove him from the line
  235. * input: head of the node
  236. * output: -
  237. */
  238. void removeFromLine(personNode** head)
  239. {
  240.     char name[STR_MAX_LEN] = { 0 };
  241.     printf("Enter name to remove:\n");
  242.     myFgets(name, STR_MAX_LEN);
  243.     personNode* p = *head;
  244.     personNode* dNode = NULL;
  245.     if (*head)
  246.     {
  247.         if (0 == strcmp((*head)->name, name))
  248.         {
  249.             *head = (*head)->next;
  250.             free(p);
  251.             printf("%s removed from line\n", name);
  252.             return;
  253.         }
  254.         else
  255.         {
  256.             while (p->next &&
  257.                 0 != strcmp(p->next->name, name))
  258.             {
  259.                 p = p->next;
  260.             }
  261.             if (p->next)
  262.             {
  263.                 dNode = p->next;
  264.                 p->next = dNode->next;
  265.                 free(dNode);
  266.                 printf("%s removed from line\n", name);
  267.                 return;
  268.             }
  269.         }
  270.     }
  271.     printf("%s not in line\n", name);
  272. }
  273. /* asks user to input guest to add him into the begin of the line (VIP client)
  274. * input: head of the main node
  275. * output: new head with the new client
  276. */
  277. personNode* addVIPToLine(personNode* head)
  278. {
  279.     personNode* newNode = NULL;
  280.  
  281.     printf("VIP GUEST!\n");
  282.  
  283.     newNode = createPersonNode();
  284.  
  285.     printf("Enter name: ");
  286.     myFgets(newNode->name, STR_MAX_LEN);
  287.  
  288.     printf("Enter age: ");
  289.     scanf("%d", &(newNode->age));
  290.     getchar();
  291.  
  292.     insertAtBegin(&head, newNode);
  293.  
  294.     return head;
  295. }
  296. /* Inserts the new node into the begin of the head node
  297. * input: head of the main node, new node
  298. * output: -
  299. */
  300. void insertAtBegin(personNode** head, personNode* newNode)
  301. {
  302.     if (!*head)
  303.     {
  304.         *head = newNode;
  305.     }
  306.     else
  307.     {
  308.         newNode->next = *head;
  309.         *head = newNode;
  310.     }
  311. }
  312. /* Reverses the given node
  313. * input: node to reverse
  314. * output: -
  315. */
  316. void reverse(personNode** head)
  317. {
  318.     personNode* prev = NULL;
  319.     personNode* current = *head;
  320.     personNode* next = NULL;
  321.  
  322.     while (current) {
  323.         // Store next
  324.         next = current->next;
  325.  
  326.         // Reverse current node's pointer
  327.         current->next = prev;
  328.  
  329.         // Move pointers one position ahead.
  330.         prev = current;
  331.         current = next;
  332.     }
  333.     *head = prev;
  334.     printf("Line reversed!\n");
  335. }
  336. /* asks user to input string to search in the given node (line)
  337. * input: head of the main node (line)
  338. * output: -
  339. */
  340. void search(personNode* head)
  341. {
  342.     char searchFor[STR_MAX_LEN ] = { 0 };
  343.     printf("Enter name to search:\n");
  344.     myFgets(searchFor, STR_MAX_LEN);
  345.    
  346.     personNode* current = head;  // Initialize current
  347.     while (current != NULL)
  348.     {
  349.         if (strcmp(current->name, searchFor) == 0)
  350.         {
  351.             printf("%s in line\n", searchFor);
  352.         }
  353.         current = current->next;
  354.     }
  355.     printf("%s not in line\n", searchFor);
  356. }
  357. /* Frees the memory of the given node (line)
  358. * input: head of the main node to free his memory
  359. * output: -
  360. */
  361. void freeLine(personNode* head)
  362. {
  363.     personNode* tmp;
  364.  
  365.     while (head)
  366.     {
  367.         tmp = head;
  368.         head = head->next;
  369.         free(tmp);
  370.     }
  371. }
  372.  
  373.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement