Advertisement
cdraugr-

kr03-4main.c

Feb 23rd, 2020
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct Elem
  5. {
  6.     struct Elem *next;
  7.     char *str;
  8. };
  9.  
  10. struct Elem *dup_elem(struct Elem *head);
  11.  
  12. int main(void)
  13. {
  14.     struct Elem *head = calloc(1, sizeof(*head));  // "10" "5x" "alpha" " -03"
  15.     head->str = calloc(100, sizeof(*head->str));
  16.     strcpy(head->str, "10");
  17.  
  18.     head->next = calloc(1, sizeof(*head));  // 5x" "alpha" " -03"
  19.     head->next->str = calloc(100, sizeof(*head->str));
  20.     strcpy(head->next->str, "5x");
  21.  
  22.     head->next->next = calloc(1, sizeof(*head));  // "alpha" " -03"
  23.     head->next->next->str = calloc(100, sizeof(*head->str));
  24.     strcpy(head->next->next->str, "alpha");
  25.  
  26.     head->next->next->next = calloc(1, sizeof(*head));  // " -03"
  27.     head->next->next->next->str = calloc(100, sizeof(*head->str));
  28.     strcpy(head->next->next->next->str, " -03");
  29.  
  30.     head->next->next->next->next = NULL;
  31.  
  32.     for (struct Elem *current_elem = head; current_elem; current_elem = current_elem->next;) {
  33.         printf("[%s]%s", current_elem->str, current_elem->next ? " -> " : "\n");
  34.     }
  35.  
  36.     printf("\n");
  37.     head = dup_elem(head);
  38.     printf("\n");
  39.  
  40.     for (struct Elem *current_elem = head; current_elem;) {
  41.         printf("[%s]%s", current_elem->str, current_elem->next ? " -> " : "\n");
  42.         struct Elem *tmp = current_elem;
  43.         current_elem = current_elem->next;
  44.         free(tmp->str);
  45.         free(tmp);
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement