From e6d94ab4d9259f6313cd84a33875bfc1db1e763a Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 24 Oct 2024 19:26:06 +0200 Subject: [PATCH] 15 --- cv4/a_train.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index e2e03f5..3d7d3de 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -39,11 +39,38 @@ void print_train(struct car* first) { } void cancel_train(struct car* first) { - + struct car* temp; + while (first != NULL) { + temp = first; + first = first->next; + free(temp); + } + printf("Vsetky vozne boli zrusene.\n"); } - struct car* clear_train(struct car* first, const char* target) { - return NULL; + struct car* current = first; + struct car* previous = NULL; + + while (current != NULL) { + // Porovnanie hodnoty cielovej stanice + if (strcmp(current->value, target) == 0) { + // Ak sa cielova stanica zhoduje, odstráni vozeň + if (previous == NULL) { + // Ak je to prvý vozeň + first = current->next; + } else { + previous->next = current->next; + } + struct car* temp = current; + current = current->next; + free(temp); + } else { + // Ak sa nezhoduje, prejde na nasledujúci vozeň + previous = current; + current = current->next; + } + } + + return first; } -