#include "a_train.h" #include #include #include //pridaj vozen na koniec spojkového zoznamu struct car* add_car(struct car* first, const char* target) { // alokacia noveho prvku struct car* newcar = calloc(1, sizeof(struct car)); if (newcar == NULL) { fprintf(stderr, "Chyba: nepodarilo sa alokovať pamäť pre nový vozeň.\n"); return first; } // kopirujeme cielovu stanicu strcpy(newcar->value, target); newcar->next = NULL; // vlak prazdny -> novy vozen je prvy if (first == NULL) { return newcar; } // inak pridame na koniec struct car* this = first; while (this->next != NULL) { this = this->next; } this->next = newcar; return first; } // vypis voznov void print_train(struct car* first) { struct car* this = first; while (this != NULL) { printf("%s\n", this->value); this = this->next; } } // zrusit vlak a uvolnit pamat void cancel_train(struct car* first) { if (first == NULL) return; // Rekurzívne uvoľnenie cancel_train(first->next); free(first); } // odstranit vozne s target stanicou 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) { // vymazat zaznam struct car* to_delete = current; current = current->next; if (prev == NULL) { // vymazat prvy prvok first = current; } else { prev->next = current; } free(to_delete); } else { // Pokračujeme ďalej prev = current; current = current->next; } } return first; }