diff --git a/cv5/a_train.c b/cv5/a_train.c index 663032b..5906938 100644 --- a/cv5/a_train.c +++ b/cv5/a_train.c @@ -62,24 +62,25 @@ struct car* clear_train(struct car* first, const char* target) { return first; } } - struct car* this = first; - while(this->next != NULL){ - int x = strcmp(this->value, target); - if(x == 0){ - struct car* third = this->next->next; - if(first == this){ - first = this->next; - free(this); - return first; - } - else { - free(this->next); - this->next = third; - } + struct car* prev = first; + while(prev->next->next != NULL){ + int x = strcmp(prev->value, target); + if(x == 0){ + first = NULL; + first = prev->next; + free(prev); + } - this = this->next; - } - + x = strcmp(prev->next->value, target); + if(x == 0){ + if(first->next->next == NULL) { + //struct car* tmp = prev->next; + free(prev->next); + prev->next = NULL; + } + } + prev = prev->next; + } return first; }