diff --git a/cv4/a_train.c b/cv4/a_train.c index 5e87327..c117b1a 100644 --- a/cv4/a_train.c +++ b/cv4/a_train.c @@ -33,37 +33,40 @@ void print_train(struct car* first) { } void cancel_train(struct car* first) { - // struct car* current = first; - // while(first != NULL){ - // current = current->next; - // free(first); - // first = current; - // } + struct car* current = first; + while(first != NULL){ + current = current->next; + free(first); + first = current; + } } struct car* clear_train(struct car* first, const char* target) { - if (first = NULL){ + if (first == NULL) { return NULL; } - struct car* current = first, new_first = first; - if (first != NULL){ - if(strcmp(first->value, target) == 0){ - new_first = new_first->next; - free(first); - first = new_first; - } + + struct car* current = first; + struct car* previous = NULL; + + while (current != NULL && strcmp(current->value, target) == 0) { + struct car* temp = current; + first = current->next; + current = first; + free(temp); } - while (first->next != NULL) { - current = first->next; + while (current != NULL) { if (strcmp(current->value, target) == 0) { - first->next = current->next; + previous->next = current->next; free(current); + current = previous->next; } else { - first = current; + previous = current; + current = current->next; } } - return new_first; -} + return first; +}