From 38d4e6a15c872a0b16b2dde064f2ddcdb80681c1 Mon Sep 17 00:00:00 2001 From: Deinerovych Date: Thu, 24 Oct 2024 17:49:05 +0200 Subject: [PATCH] 1 --- cv4/a_train.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 cv4/a_train.c diff --git a/cv4/a_train.c b/cv4/a_train.c new file mode 100644 index 0000000..e1c3f29 --- /dev/null +++ b/cv4/a_train.c @@ -0,0 +1,75 @@ +#include "a_train.h" +#include +#include +#include + +struct car* add_car(struct car* first, const char* target) { + struct car* new_car = (struct car*)malloc(sizeof(struct car)); + if (!new_car) { + return first; + } + + strncpy(new_car->value, target, SIZE); + new_car->value[SIZE - 1] = '\0'; + new_car->next = NULL; + + if (first == NULL) { + return new_car; + } + + struct car* current = first; + while (current->next != NULL) { + current = current->next; + } + + current->next = new_car; + return first; +} + +void print_train(struct car* first) { + struct car* current = first; + + if (current == NULL) { + printf("Vlak je prazdny.\n"); + return; + } + + while (current != NULL) { + printf("%s\n", current->value); + current = current->next; + } +} + +void cancel_train(struct car* first) { + struct car* current = first; + + while (current != NULL) { + struct car* next = current->next; + free(current); + current = next; + } +} + +struct car* clear_train(struct car* first, const char* target) { + struct car* current = first; + struct car* prev = NULL; + + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + struct car* to_delete = current; + if (prev == NULL) { + first = current->next; + } else { + prev->next = current->next; + } + current = current->next; + free(to_delete); + } else { + prev = current; + current = current->next; + } + } + + return first; +} +