usaa25/du4/a_train.c
2025-11-06 21:37:12 +01:00

58 lines
1.3 KiB
C

#include "a_train.h"
struct car* add_car(struct car* first, const char* target) {
struct car* newcar = malloc(sizeof(struct car));
if (!newcar) return first;
strncpy(newcar->value, target, SIZE - 1);
newcar->value[SIZE - 1] = '\0';
newcar->next = NULL;
if (first == NULL) {
return newcar;
}
struct car* last = first;
while (last->next)
last = last->next;
last->next = newcar;
return first;
}
void print_train(const struct car* first) {
for (const struct car* c = first; c; c = c->next)
printf("%s\n", c->value);
}
void cancel_train(struct car* first) {
while (first) {
struct car* tmp = first->next;
free(first);
first = tmp;
}
}
struct car* clear_train(struct car* first, const char* target) {
// Odstráni vozne zo začiatku
while (first && strcmp(first->value, target) == 0) {
struct car* tmp = first->next;
free(first);
first = tmp;
}
if (!first) return NULL;
struct car* prev = first;
while (prev->next) {
if (strcmp(prev->next->value, target) == 0) {
struct car* del = prev->next;
prev->next = del->next;
free(del);
} else {
prev = prev->next;
}
}
return first;
}