diff --git a/cv4/a_train.c b/cv4/a_train.c index 4e252b2..dd0f56f 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -76,6 +76,8 @@ struct car* clear_train(struct car* first, const char* target) } } struct car* prev = first; + struct car* bef_prev = first; + if(prev->next->next == NULL) { @@ -98,17 +100,44 @@ struct car* clear_train(struct car* first, const char* target) } } + while (prev->next->next != NULL) { - if(strcmp(prev->value, target)) + if(!strcmp(prev->value, target)) { struct car* third = prev->next->next; free(prev->next); prev->next = third; return first; } + if(prev->next->next != NULL) + { + bef_prev = prev; + } prev = prev->next; } + + if(prev->next->next == NULL) + { + if(!strcmp(prev->value, target)) + { + struct car* last = prev->next; + free(prev); + bef_prev->next = last; + return first; + } + else if(!strcmp(prev->next->value, target)) + { + free(prev->next); + prev->next = NULL; + return first; + } + else + { + return first; + } + } + return first; }