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; } -