diff --git a/cv4/a_train.c b/cv4/a_train.c index e69de29..0dd75a7 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -0,0 +1,82 @@ +#include "train.h" +#include +#include +#include + +struct car* create_car(const char* target) { + struct car* new_car = (struct car*)malloc(sizeof(struct car)); + if (new_car != NULL) { + strncpy(new_car->value, target, SIZE - 1); + new_car->value[SIZE - 1] = '\0'; + new_car->next = NULL; + } + return new_car; +} + +struct car* add_car(struct car* first, const char* target) { + struct car* new_car = create_car(target); + if (new_car == NULL) { + printf("Nedostatok pamäte pre nový vozeň!\n"); + return first; + } + + 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 prázdny.\n"); + return; + } + + while (current != NULL) { + printf("Vozeň cieľovej stanice: %s\n", current->value); + current = current->next; + } +} + +void cancel_train(struct car* first) { + struct car* current = first; + struct car* next_car; + + while (current != NULL) { + next_car = current->next; + free(current); + current = next_car; + } +} + +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) { + if (prev == NULL) { + first = current->next; + } else { + prev->next = current->next; + } + struct car* temp = current; + current = current->next; + free(temp); + } else { + prev = current; + current = current->next; + } + } + + return first; +}