From 4aeb52f77e2f76d5f0473027d5be9407c1ffbb6f Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Wed, 4 Nov 2020 22:29:07 +0100 Subject: [PATCH] clear --- cv5/a_train.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/cv5/a_train.c b/cv5/a_train.c index 1a17e3c..66268ba 100644 --- a/cv5/a_train.c +++ b/cv5/a_train.c @@ -62,23 +62,33 @@ struct car* clear_train(struct car* first, const char* target) { return first; } } - struct car* prev = first; - while(prev->next->next != NULL){ - int x = strcmp(prev->next->value, target); + struct car* this = first; + while(this->next->next != NULL){ + int x = strcmp(this->next->value, target); if(x == 0){ - struct car* third = prev->next->next; - free(prev->next); - prev->next = third; - return prev; + struct car* curr = first; + struct car* prev = NULL; + if(first == this){ + first = this->next; + free(this); + return first; + } + while(curr != this){ + prev = curr; + curr = curr->next; + } + prev->next = curr->next; + this->next = NULL; + free(this); } - prev = prev->next; + this = this->next; } - int x = strcmp(prev->next->value, target); + int x = strcmp(this->next->value, target); if(x == 0){ - free(prev->next); - prev->next = NULL; + free(this->next); + this->next = NULL; return first; } return first;