Advertisement
Yonka2019

question2.c

Jun 1st, 2021
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.80 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define STR_MAX_LEN 20
  8.  
  9. typedef struct personNode {
  10.     char name[STR_MAX_LEN];
  11.     int age;
  12.  
  13.     struct personNode* next;
  14. } personNode;
  15.  
  16. void myFgets(char str[], int n);
  17. void printList(personNode* head);
  18. void freeList(personNode* head);
  19. void insertAtEnd(personNode** head, personNode* newNode);
  20. int listLength(personNode* head);
  21. personNode* createPersonNode();
  22. personNode* getList(personNode* head, int size);
  23. personNode* copyList(personNode* head);
  24.  
  25.  
  26. int main()
  27. {
  28.     int peoples_number = 0;
  29.     personNode* head = NULL;
  30.     personNode* duplicate = NULL;
  31.  
  32.     printf("How many people in the list? ");
  33.     scanf("%d", &peoples_number);
  34.     getchar();
  35.  
  36.     head = getList(head, peoples_number);
  37.     printList(head);
  38.  
  39.     printf("List length: %d\n", listLength(head));
  40.  
  41.     duplicate = copyList(head);
  42.     printf("New list:\n");
  43.     printList(duplicate);
  44.  
  45.     freeList(head);
  46.     freeList(duplicate);
  47.  
  48.     getchar();
  49.     return 0;
  50. }
  51. /* Inserts the given node into the end of the head (main) node
  52. * input: head of the main node, new node
  53. * output: -
  54. */
  55. void insertAtEnd(personNode** head, personNode* newNode)
  56. {
  57.     if (!*head)
  58.     {
  59.         *head = newNode;
  60.     }
  61.     else
  62.     {
  63.         personNode* p = *head;
  64.         while (p->next)
  65.         {
  66.             p = p->next;
  67.         }
  68.         p->next = newNode;
  69.     }
  70. }
  71. /* Creates new node of person
  72. * input: -
  73. * output: new node of person struct type
  74. */
  75. personNode* createPersonNode()
  76. {
  77.     personNode* newPersonNode = (personNode*)malloc(sizeof(personNode));
  78.     strcpy(newPersonNode->name, "No name");
  79.     newPersonNode->age = 0;
  80.     newPersonNode->next = NULL;
  81.  
  82.     return newPersonNode;
  83. }
  84. /* Custom fgets with built-in '\n' remove
  85. * input: string to in-put, string size
  86. * output: -
  87. */
  88. void myFgets(char str[], int n)
  89. {
  90.     fgets(str, n, stdin);
  91.     str[strcspn(str, "\n")] = 0;
  92. }
  93. /* Prints the given list
  94. * input: head of the main node (list)
  95. * output: -
  96. */
  97. void printList(personNode* head)
  98. {
  99.     personNode* current_node = head;
  100.  
  101.     while (current_node != NULL)
  102.     {
  103.         printf("Name: %s, Age: %d\n", current_node->name, current_node->age);
  104.         current_node = current_node->next;
  105.     }
  106. }
  107. /* Counts the length of the given list
  108. * input: head of the main node (list)
  109. * output: length of the given list
  110. */
  111. int listLength(personNode* head)
  112. {
  113.     if (!head)  // if list empty
  114.     {
  115.         return 0;
  116.     }
  117.     else
  118.     {
  119.         return 1 + listLength(head->next);
  120.     }
  121. }
  122. /* Return duplicate of the given list
  123. * input: head of the main node (list)
  124. * output: NEW; ALLOCATED in memory list duplicate of the given 'head' node (list)
  125. */
  126. personNode* copyList(personNode* head)
  127. {
  128.     if (!head)  // if list empty
  129.     {
  130.         return NULL;
  131.     }
  132.     else
  133.     {
  134.  
  135.         // Allocate the memory for new Node
  136.         // in the heap and set its data
  137.         personNode* newNode = (personNode*)malloc(sizeof(personNode));
  138.  
  139.         strcpy(newNode->name, head->name);
  140.         newNode->age = head->age;
  141.  
  142.         // Recursively set the next pointer of
  143.         // the new Node by recurring for the
  144.         // remaining nodes
  145.         newNode->next = copyList(head->next);
  146.  
  147.         return newNode;
  148.     }
  149. }
  150. /* Asks the user to input the name and age according the given number of peoples
  151. * input: head of the main node, number of peoples
  152. * output: new node of personNode struct type
  153. */
  154. personNode* getList(personNode* head, int size)
  155. {
  156.     int i = 0;
  157.     personNode* newNode = NULL;
  158.  
  159.     for (i = 0; i < size; i++)
  160.     {
  161.         newNode = createPersonNode();
  162.  
  163.         printf("Enter name: ");
  164.         myFgets(newNode->name, STR_MAX_LEN);
  165.  
  166.         printf("Enter age: ");
  167.         scanf("%d", &(newNode->age));
  168.         getchar();
  169.  
  170.         insertAtEnd(&head, newNode);
  171.     }
  172.  
  173.     return head;
  174. }
  175. /* Frees the memory that the node used
  176. * input: the head of the main node (list)
  177. * output: -
  178. */
  179. void freeList(personNode* head)
  180. {
  181.     personNode* tmp;
  182.  
  183.     while (head)
  184.     {
  185.         tmp = head;
  186.         head = head->next;
  187.         free(tmp);
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement