#include "a_train.h" #include #include #include struct car* add_car(struct car* first, const char* target) { struct car* newcar = (struct car*)calloc(1, sizeof(struct car)); if (newcar == NULL) { printf("Chyba: nepodarilo sa alokovať pamäť.\n"); exit(1); } strcpy(newcar->value, target); newcar->next = NULL; if (first == NULL) { return newcar; } struct car* current = first; while (current->next != NULL) { current = current->next; } current->next = newcar; 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("%s\n", current->value); current = current->next; } } void cancel_train(struct car* first) { if (first == NULL) { return; } cancel_train(first->next); free(first); } struct car* clear_train(struct car* first, const char* target) { struct car* prev = NULL; struct car* current = first; while (current != NULL) { if (strcmp(current->value, target) == 0) { if (prev == NULL) { struct car* temp = current->next; free(current); current = temp; first = current; } else { prev->next = current->next; free(current); current = prev->next; } } else { prev = current; current = current->next; } } return first; }