Advertisement
Guest User

Untitled

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