From 2a249097a5eb5c07668c2c2a3e12187c57d200aa Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 4 Nov 2020 11:08:03 +0100 Subject: [PATCH] clear --- cv5/a_train.c | 64 +++++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/cv5/a_train.c b/cv5/a_train.c index 48baf0d..cf81c20 100644 --- a/cv5/a_train.c +++ b/cv5/a_train.c @@ -57,51 +57,29 @@ struct car* clear_train(struct car* first, const char* target) { if(result == 0){ free(first); return 0; - } + } + else{ + return first; + } } - //struct car* this = first; - /*if(first->next->next == NULL){ - int x = strcmp(this->next->value, target); - if(x == 0){ - struct car* secondlast = first; - while(this->next != NULL){ - secondlast = this; - this = this->next; - } - if(this == first){ - first = NULL; - } - else{ - secondlast->next = NULL; - } - free(this); - } - } */ - - else{ - struct car* this = first; - while(this->next!= NULL){ - int x = strcmp(this->value, target); + struct car* prev = first; + while(prev->next->next != NULL){ + int x = strcmp(prev->next->value, target); if(x == 0){ - struct car* curr = first; - struct car* prev = NULL; - if(first == this){ - first = this->next; - free(this); - //first->next = NULL; - return first; - } - while(curr != this){ - prev = curr; - curr = curr->next; - - } - prev->next = curr->next; - this->next = NULL; - free(this); + struct car* third = prev->next->next; + free(prev->next); + prev->next = third; + return first; } - this = this->next; + prev = prev->next; } - } - return first; + int x = strcmp(prev->next->value, target); + if(x == 0){ + free(prev->next); + prev->next = NULL; + return first; + + } + return first; + }