diff --git a/du4/a_train.c b/du4/a_train.c index c7bb6fd..6e58901 100644 --- a/du4/a_train.c +++ b/du4/a_train.c @@ -42,45 +42,24 @@ void cancel_train(struct car* first) { struct car* clear_train(struct car* first, const char* target) { - if(first == NULL){ - return NULL; - } - if (first->next == NULL && strcmp(first->value, target) == 0) - { - free(first); - return NULL; - } - else if(first->next == NULL && strcmp(first->value, target) != 0){ - return first; - } - if(first->next->next == NULL && strcmp(first->value, target) == 0){ - struct car* temp = first; - first = first->next; - first->next = NULL; - free(temp); - return first; + struct car* curr = first; + struct car* prev = NULL; + while (curr) { + if (strcmp(curr->value, target) == 0) { + struct car* to_delete = curr; + if (prev) + prev->next = curr->next; + else + first = curr->next; + curr = curr->next; + free(to_delete); + } else { + prev = curr; + curr = curr->next; + } } - struct car* prev= NULL; - struct car* temp = first; - while (temp->next) - { - prev = temp; - temp = temp->next; - if(strcmp(temp->value, target) == 0){ - struct car* targetNode = temp; - prev->next = temp->next; - temp = temp->next; - free(targetNode); - } - else{ - prev = temp; - temp = temp->next; - } - - } - - return first; + return first; }