From e14db80e397a971a8ce6d24be67b14b5247cf349 Mon Sep 17 00:00:00 2001 From: Anzhelika Nikolaieva Date: Tue, 24 Oct 2023 18:23:56 +0000 Subject: [PATCH] Update 'cv4/a_train.c' --- cv4/a_train.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/cv4/a_train.c b/cv4/a_train.c index e69de29..d919bda 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -0,0 +1,67 @@ +#include "train.h" +#include +#include +#include + +struct car* add_car(struct car* first, const char* target) { + struct car* new_car = (struct car*)calloc(1, sizeof(struct car)); + if (new_car == NULL) { + return first; + } + + strcpy(new_car->value, target); + new_car->next = NULL; + + if (first == NULL) { + return new_car; + } else { + struct car* current = first; + while (current->next != NULL) { + current = current->next; + } + current->next = new_car; + return first; + } +} + +void print_train(struct car* first) { + struct car* current = first; + while (current != NULL) { + printf("%s\n", current->value); + current = current->next; + } +} + +void cancel_train(struct car* first) { + while (first != NULL) { + struct car* current = first; + first = first->next; + free(current); + } +} + +struct car* clear_train(struct car* first, const char* target) { + struct car* new_first = first; + struct car* prev = NULL; + struct car* current = first; + + while (current != NULL) { + if (strcmp(current->value, target) == 0) { + if (prev == NULL) { + new_first = current->next; + free(current); + current = new_first; + } else { + prev->next = current->next; + free(current); + current = prev->next; + } + + } else { + prev = current; + current = current->next; + } + } + + return new_first; +}