Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- // Описание структуры MusicalComposition
- typedef struct MusicalComposition {
- char *name;
- char *author;
- int year;
- struct MusicalComposition *next;
- struct MusicalComposition *previous;
- } MusicalComposition;
- // Создание структуры MusicalComposition
- MusicalComposition *createMusicalComposition(char *name, char *author, int year) {
- MusicalComposition *lst = (MusicalComposition *) malloc(sizeof(MusicalComposition));
- lst->name = name;
- lst->author = author;
- lst->year = year;
- lst->next = NULL;
- lst->previous = NULL;
- return lst;
- };
- // Функции для работы со списком MusicalComposition
- MusicalComposition *createMusicalCompositionList(char **array_names, char **array_authors, int *
- array_years, int n) {
- MusicalComposition *prev;
- MusicalComposition *head;
- MusicalComposition *lst;
- for (int i = 0; i < n; i++) {
- lst = createMusicalComposition(array_names[i], array_authors[i], array_years[i]);
- lst->name = array_names[i];
- lst->author = array_authors[i];
- lst->year = array_years[i];
- if ((i != 0) && (i != n - 1)) {
- lst->previous = prev;
- prev->next = lst;
- prev = lst;
- } else {
- if (i == 0) {
- head = lst;
- prev = lst;
- } else {
- lst->previous = prev;
- prev->next = lst;
- }
- }
- }
- return head;
- };
- void push(MusicalComposition *head, MusicalComposition *element) {
- while (head->next != NULL) {
- head = head->next;
- }
- head->next = element;
- head->next->previous = head;
- };
- void removeEl(MusicalComposition *head, char *name_for_remove) {
- while (head != NULL) {
- if (strcmp(head->name, name_for_remove) == 0) {
- if (head->previous != NULL) {
- head->previous->next = head->next;
- }
- if (head->next != NULL) {
- head->next->previous = head->previous;
- }
- }
- head = head->next;
- }
- };
- int count(MusicalComposition *head) {
- int kl = 0;
- while (head != NULL) {
- kl++;
- head = head->next;
- }
- return kl;
- };
- void print_names(MusicalComposition *head){
- while(head != NULL){
- printf("%s\n", head->name);
- head = head->next;
- }
- };
- int main() {
- int length;
- scanf("%d\n", &length);
- char **names = (char **) malloc(sizeof(char *) * length);
- char **authors = (char **) malloc(sizeof(char *) * length);
- int *years = (int *) malloc(sizeof(int) * length);
- for (int i = 0; i < length; i++) {
- char name[80];
- char author[80];
- fgets(name, 80, stdin);
- fgets(author, 80, stdin);
- fscanf(stdin, "%d\n", &years[i]);
- (*strstr(name, "\n")) = 0;
- (*strstr(author, "\n")) = 0;
- names[i] = (char *) malloc(sizeof(char *) * (strlen(name) + 1));
- authors[i] = (char *) malloc(sizeof(char *) * (strlen(author) + 1));
- strcpy(names[i], name);
- strcpy(authors[i], author);
- }
- MusicalComposition *head = createMusicalCompositionList(names, authors, years, length);
- char name_for_push[80];
- char author_for_push[80];
- int year_for_push;
- char name_for_remove[80];
- fgets(name_for_push, 80, stdin);
- fgets(author_for_push, 80, stdin);
- fscanf(stdin, "%d\n", &year_for_push);
- (*strstr(name_for_push, "\n")) = 0;
- (*strstr(author_for_push, "\n")) = 0;
- MusicalComposition *element_for_push = createMusicalComposition(name_for_push, author_for_push,
- year_for_push);
- fgets(name_for_remove, 80, stdin);
- (*strstr(name_for_remove, "\n")) = 0;
- printf("%s %s %d\n", head->name, head->author, head->year);
- int k = count(head);
- printf("%d\n", k);
- push(head, element_for_push);
- k = count(head);
- printf("%d\n", k);
- removeEl(head, name_for_remove);
- print_names(head);
- k = count(head);
- printf("%d\n", k);
- for (int i = 0; i < length; i++) {
- free(names[i]);
- free(authors[i]);
- }
- free(names);
- free(authors);
- free(years);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement