From 64d9d7bfa391348092fa9fe82f727faae690dcbb Mon Sep 17 00:00:00 2001 From: Yurii Yakovenko Date: Wed, 23 Oct 2024 19:35:27 +0000 Subject: [PATCH] Update cv4/a_train.c --- cv4/a_train.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 9 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index 3ecfbc8..df08cb0 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -1,18 +1,111 @@ -#include "a_train.h" #include +#define SIZE 50 -struct car* add_car(struct car* first,const char* target) { - return NULL; + +struct car +{ + char value[SIZE]; // Cieľová stanica + struct car* next; // Ďaľší záznam, môže byť NULL +}; + +struct car* add_car(struct car* first,const char* target) +{ + struct car* nc=(struct car*) malloc(sizeof(struct car)); + strcpy(nc->value, target); + nc->next=NULL; + + if(first) + { + while(first->next!=NULL) + { + first=first->next; + } + first->next=nc; + return first; + } + + first=nc; + + return first; } -void print_train(struct car* first) { +void print_train(struct car* first) +{ + if(first==NULL) + { + return; + } + + do + { + printf("%s -> ", first->value); + first=first->next; + }while(first->next!=NULL); + } -void cancel_train(struct car* first) { +void cancel_train(struct car* first) +{ + struct car* t; + while(first!=NULL) + { + t=first; + first=first->next; + free(t); + } +} + +struct car* clear_train(struct car* first, const char* target) +{ + struct car* t; //змінна для збереження кого видаляти + + if(first==NULL || target==NULL) + { + return first; + } + + //голова == таргет - видаляємо лише перший + while(strcmp(first->value, target)==0) //поки рядки однакові + { + t=first; + first=first->next; + free(t); + if(first==NULL) + { + return NULL; + } + } + + //голова не таргет + do + { + if(strcmp(first->next->value, target)==0) + { + t=first; + first=first->next; + free(t); + } + else + { + first=first->next; + } + } + while(first); + return first; +} + +int main() + { + struct car* train = NULL; + train = add_car(train,"Presov"); + train = add_car(train,"Bratislava"); + train = add_car(train,"Levoca"); + train = add_car(train,"Spiska Nova Ves"); + print_train(train); + clear_train(train,"Levoca"); + print_train(train); + cancel_train(train); + return 0; } -struct car* clear_train(struct car* first, const char* target) { - return NULL; -} -