From a3f63c348e83cfe54af0cd771ce0d4e28eda8916 Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 24 Oct 2024 18:56:50 +0200 Subject: [PATCH] 1 --- cv4/a_train.h | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/cv4/a_train.h b/cv4/a_train.h index 5982bfe..5708138 100644 --- a/cv4/a_train.h +++ b/cv4/a_train.h @@ -5,15 +5,32 @@ /** * Jeden vozen vlaku */ -struct car { - /** - * Nazov cielovej stanice - */ - char value[SIZE]; - /** - * Smenik na dalsi vozen - */ - struct car* next; +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; }; /**