From ee01429d409467ae076325b4a619a9fe5bd55850 Mon Sep 17 00:00:00 2001 From: Maryna Kravtsova Date: Tue, 3 Nov 2020 20:43:01 +0100 Subject: [PATCH] clear --- cv5/a_train.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/cv5/a_train.c b/cv5/a_train.c index baf7e72..6bb7c5c 100644 --- a/cv5/a_train.c +++ b/cv5/a_train.c @@ -64,17 +64,20 @@ struct car* clear_train(struct car* first, const char* target) { while(first != NULL){ int x = strcmp(this->value, target); if(x == 0){ - struct car* prev = this; - struct car* node = this->next; - while(prev != NULL && node != NULL){ - prev->next = node->next; - free(node); - prev = prev->next; - if(prev != NULL){ - node = prev->next; - return 0; - } + struct car* curr = first; + struct car* prev = NULL; + if(first == this){ + first = this->next; + free(this); + return 0; } + while(curr != this){ + prev = curr; + curr = curr->next; + + } + prev->next = curr->next; + free(this); } this = this->next; }