From 8897fa16b1f3b97e7dd2f9ac50dd02d4619f8a35 Mon Sep 17 00:00:00 2001 From: Yurii Yakovenko Date: Thu, 24 Oct 2024 21:02:16 +0000 Subject: [PATCH] Update cv4/a_train.c --- cv4/a_train.c | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/cv4/a_train.c b/cv4/a_train.c index a3e46e1..336c569 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -3,11 +3,16 @@ +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* ret = first; - struct car* nc=(struct car*) malloc(sizeof(struct car)); + struct car* ret=first; + struct car* nc=(struct car*) malloc(sizeof(struct car)); strcpy(nc->value, target); nc->next=NULL; @@ -30,6 +35,7 @@ void print_train(struct car* first) { if(first==NULL) { + return; } @@ -37,9 +43,7 @@ void print_train(struct car* first) { printf("%s", first->value); first=first->next; - if(first){ - printf("->"); - } + if(first) printf("->"); }while(first!=NULL); } @@ -57,43 +61,45 @@ void cancel_train(struct car* first) struct car* clear_train(struct car* first, const char* target) { - struct car* t; //змінна для збереження кого видаляти + struct car* t; + struct car* ptr; if(first==NULL || target==NULL) { return first; } - - //голова == таргет - видаляємо лише перший - while(strcmp(first->value, target)==0) //поки рядки однакові + + while(strcmp(first->value, target)==0) { t=first; first=first->next; free(t); + if(first==NULL) { + return NULL; - } + } } - //голова не таргет + + ptr=first; + do { - if(strcmp(first->next->value, target)==0) + if(strcmp(ptr->next->value, target)==0) { - t=first; - first=first->next; + t=ptr->next;; + ptr->next = ptr -> next->next; free(t); } else { - first=first->next; + ptr = ptr -> next; } - } - while(first); + }while(ptr->next); + + return first; } - - -