diff --git a/cv4/a_train.c b/cv4/a_train.c new file mode 100644 index 0000000..3102ea1 --- /dev/null +++ b/cv4/a_train.c @@ -0,0 +1,80 @@ +#include "a_train.h" +#include +#include +#include + + +struct car* add_car(struct car* first, const char* target) { + struct car* newcar = (struct car*)calloc(1, sizeof(struct car)); + if (newcar == NULL) { + printf("Ошибка: не удалось выделить память.\n"); + exit(1); + } + + strcpy(newcar->value, target); + newcar->next = NULL; + + if (first == NULL) { + return newcar; + } + + struct car* current = first; + while (current->next != NULL) { + current = current->next; + } + + current->next = newcar; + return first; +} + + +void print_train(struct car* first) { + struct car* current = first; + + if (current == NULL) { + printf("Поезд пуст.\n"); + return; + } + + while (current != NULL) { + printf("%s\n", current->value); + current = current->next; + } +} + + +void cancel_train(struct car* first) { + if (first == NULL) { + return; + } + + cancel_train(first->next); + free(first); +} + + +struct car* clear_train(struct car* first, const char* target) { + struct car* prev = NULL; + struct car* current = first; + + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + if (prev == NULL) { + struct car* temp = current->next; + free(current); + current = temp; + first = current; + } else { + prev->next = current->next; + free(current); + current = prev->next; + } + } else { + prev = current; + current = current->next; + } + } + + return first; +} +