diff --git a/du4/a_train.c b/du4/a_train.c index 6e58901..5bc9dcf 100644 --- a/du4/a_train.c +++ b/du4/a_train.c @@ -42,24 +42,43 @@ void cancel_train(struct car* first) { struct car* clear_train(struct car* first, const char* target) { - 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; - } + 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; - return first; + } + struct car* prev= NULL; + struct car* temp = first; + while (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; }