From 00ac4ffb5abb87685103a33e57fd905e624ceb77 Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 24 Oct 2024 19:05:08 +0200 Subject: [PATCH] 3 --- cv4/a_train.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index f530fbc..5e59cc4 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -1,8 +1,34 @@ #include "a_train.h" #include +#include +#include -struct car* add_car(struct car* first,const char* target) { - return NULL; +struct car* add_car(struct car* first, const char* target) { + // Vytvorenie noveho vozna + struct car* new_car = (struct car*)malloc(sizeof(struct car)); + if (new_car == NULL) { + printf("Chyba: Nepodarilo sa alokovať pamäť pre nový vozeň.\n"); + return first; + } + + // Inicializacia hodnot + strncpy(new_car->value, target, SIZE); + new_car->next = NULL; + + // Ak vlak neexistuje, novy vozen sa stane prvym + if (first == NULL) { + return new_car; + } + + // Najdenie posledneho vozna + struct car* temp = first; + while (temp->next != NULL) { + temp = temp->next; + } + + // Pridanie noveho vozna na koniec + temp->next = new_car; + return first; } void print_train(struct car* first) {