From ed44d7bd3cb57938b4ea3e1bcde9c6fbfb8120d4 Mon Sep 17 00:00:00 2001 From: VIliam Date: Thu, 24 Oct 2024 19:14:24 +0200 Subject: [PATCH] 6 --- cv4/a_train.c | 12 +++--------- cv4/a_train.h | 38 ++++++++------------------------------ 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index c7c4661..e2e03f5 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -4,30 +4,24 @@ #include 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; } diff --git a/cv4/a_train.h b/cv4/a_train.h index 6ee9e21..8458370 100644 --- a/cv4/a_train.h +++ b/cv4/a_train.h @@ -1,9 +1,8 @@ -#include -#include #ifndef TRAIN_H #define TRAIN_H #define SIZE 100 +#include /** * Jeden vozen vlaku */ @@ -18,34 +17,13 @@ struct car { 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; -} +/** + * Prida vozen na koniec vlaku. + * + * @arg nazov cielovej stanice, ktory sa ma priradit novemu voznu. + * @return smernik na zaciatok vlaku. + */ +struct car* add_car(struct car* first,const char* target); /**