From e84146551bc51b65256b4b68b2fe414294696f6f Mon Sep 17 00:00:00 2001 From: Kozar Date: Tue, 15 Oct 2024 13:43:00 +0000 Subject: [PATCH] Initializacia --- cv4/a_train.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index 3ecfbc8..edd3bfb 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -1,18 +1,67 @@ #include "a_train.h" #include +#include +#include struct car* add_car(struct car* first,const char* target) { - return NULL; + struct car* last = first; + struct car* newcar* = calloc(1, sizeof(struct car)); + strcpy(newcar->value, target); + if (last != NULL){ + struct car* current = last; + while(current->next != NULL){ + current = current->next + } + current->next = newcar; + } else{ + last = newcar; + return last; + } + return first; } void print_train(struct car* first) { + if(first != NULL){ + struct car* current = first; + while(current->next != NULL){ + printf("%s\n", current->value); + current = current->next; + } + } } void cancel_train(struct car* first) { + struct car* current = first; + while(first != NULL){ + current = current->next; + free(first); + first = current; + } } struct car* clear_train(struct car* first, const char* target) { - return NULL; + if (first = NULL){ + return NULL; + } + struct car* current = first, new_first = first; + if (first != NULL){ + if(strcmp(first->destination, target) == 0){ + new_first = new_first->next; + free(first); + first = new_first; + } + } + + while (first->next != NULL) { + current = first->next; + if (strcmp(current->destination, target) == 0) { + first->next = current->next; + free(current); + } else { + first = current; + } + } + return new_first; }