Advertisement
LilChicha174

Untitled

Feb 27th, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.40 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5.  
  6. // Описание структуры MusicalComposition
  7.  
  8. typedef struct MusicalComposition {
  9.     char *name;
  10.     char *author;
  11.     int year;
  12.     struct MusicalComposition *next;
  13.     struct MusicalComposition *previous;
  14. } MusicalComposition;
  15.  
  16. // Создание структуры MusicalComposition
  17.  
  18. MusicalComposition *createMusicalComposition(char *name, char *author, int year) {
  19.     MusicalComposition *lst = (MusicalComposition *) malloc(sizeof(MusicalComposition));
  20.     lst->name = name;
  21.     lst->author = author;
  22.     lst->year = year;
  23.     lst->next = NULL;
  24.     lst->previous = NULL;
  25.     return lst;
  26. };
  27.  
  28. // Функции для работы со списком MusicalComposition
  29.  
  30. MusicalComposition *createMusicalCompositionList(char **array_names, char **array_authors, int *
  31. array_years, int n) {
  32.     MusicalComposition *prev;
  33.     MusicalComposition *head;
  34.     MusicalComposition *lst;
  35.     for (int i = 0; i < n; i++) {
  36.         lst = createMusicalComposition(array_names[i], array_authors[i], array_years[i]);
  37.         lst->name = array_names[i];
  38.         lst->author = array_authors[i];
  39.         lst->year = array_years[i];
  40.         if ((i != 0) && (i != n - 1)) {
  41.             lst->previous = prev;
  42.             prev->next = lst;
  43.             prev = lst;
  44.         } else {
  45.             if (i == 0) {
  46.                 head = lst;
  47.                 prev = lst;
  48.             } else {
  49.                 lst->previous = prev;
  50.                 prev->next = lst;
  51.             }
  52.         }
  53.     }
  54.     return head;
  55. };
  56.  
  57. void push(MusicalComposition *head, MusicalComposition *element) {
  58.     while (head->next != NULL) {
  59.         head = head->next;
  60.     }
  61.     head->next = element;
  62.     head->next->previous = head;
  63. };
  64.  
  65. void removeEl(MusicalComposition *head, char *name_for_remove) {
  66.     while (head != NULL) {
  67.         if (strcmp(head->name, name_for_remove) == 0) {
  68.             if (head->previous != NULL) {
  69.                 head->previous->next = head->next;
  70.             }
  71.             if (head->next != NULL) {
  72.                 head->next->previous = head->previous;
  73.             }
  74.         }
  75.         head = head->next;
  76.     }
  77. };
  78.  
  79. int count(MusicalComposition *head) {
  80.     int kl = 0;
  81.     while (head != NULL) {
  82.         kl++;
  83.         head = head->next;
  84.     }
  85.     return kl;
  86. };
  87.  
  88. void print_names(MusicalComposition *head){
  89.     while(head != NULL){
  90.         printf("%s\n", head->name);
  91.         head = head->next;
  92.     }
  93. };
  94.  
  95.  
  96. int main() {
  97.     int length;
  98.     scanf("%d\n", &length);
  99.  
  100.     char **names = (char **) malloc(sizeof(char *) * length);
  101.     char **authors = (char **) malloc(sizeof(char *) * length);
  102.     int *years = (int *) malloc(sizeof(int) * length);
  103.  
  104.     for (int i = 0; i < length; i++) {
  105.         char name[80];
  106.         char author[80];
  107.  
  108.         fgets(name, 80, stdin);
  109.         fgets(author, 80, stdin);
  110.         fscanf(stdin, "%d\n", &years[i]);
  111.  
  112.         (*strstr(name, "\n")) = 0;
  113.         (*strstr(author, "\n")) = 0;
  114.  
  115.         names[i] = (char *) malloc(sizeof(char *) * (strlen(name) + 1));
  116.         authors[i] = (char *) malloc(sizeof(char *) * (strlen(author) + 1));
  117.  
  118.         strcpy(names[i], name);
  119.         strcpy(authors[i], author);
  120.  
  121.     }
  122.     MusicalComposition *head = createMusicalCompositionList(names, authors, years, length);
  123.  
  124.     char name_for_push[80];
  125.     char author_for_push[80];
  126.     int year_for_push;
  127.  
  128.     char name_for_remove[80];
  129.  
  130.     fgets(name_for_push, 80, stdin);
  131.     fgets(author_for_push, 80, stdin);
  132.     fscanf(stdin, "%d\n", &year_for_push);
  133.     (*strstr(name_for_push, "\n")) = 0;
  134.     (*strstr(author_for_push, "\n")) = 0;
  135.  
  136.     MusicalComposition *element_for_push = createMusicalComposition(name_for_push, author_for_push,
  137.                                                                     year_for_push);
  138.  
  139.     fgets(name_for_remove, 80, stdin);
  140.     (*strstr(name_for_remove, "\n")) = 0;
  141.  
  142.     printf("%s %s %d\n", head->name, head->author, head->year);
  143.     int k = count(head);
  144.  
  145.     printf("%d\n", k);
  146.     push(head, element_for_push);
  147.  
  148.     k = count(head);
  149.     printf("%d\n", k);
  150.  
  151.     removeEl(head, name_for_remove);
  152.     print_names(head);
  153.  
  154.     k = count(head);
  155.     printf("%d\n", k);
  156.  
  157.     for (int i = 0; i < length; i++) {
  158.         free(names[i]);
  159.         free(authors[i]);
  160.     }
  161.     free(names);
  162.     free(authors);
  163.     free(years);
  164.  
  165.     return 0;
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement