This commit is contained in:
Oleksandr Vyshniakov 2025-11-04 19:05:29 +01:00
parent af19fc388a
commit af6e9fefd6
2 changed files with 22 additions and 20 deletions

View File

@ -58,30 +58,32 @@ void cancel_train (struct car* first) {
}
struct car* clear_train(struct car* first, const char* target) {
if (first == NULL) {
return NULL;
}
struct car* current = first; // текущий вагон
struct car* previous = NULL; // предыдущий вагон
struct car* current = first;
struct car* previous = NULL;
while (current != NULL) {
if (strcmp(current->value, target) == 0) {
struct car* to_delete = current;
if (previous == NULL) {
struct car* new_start = current->next; //Сохраняем адрес второго вагона
free(current);
current = new_start; //Двигаем курсор на следующий вагон
first = new_start; //Обновляем начало поезда
continue;
first = current->next;
current = first;
}
} else {
else {
previous->next = current->next;
free(current);
current = previous->next;
continue;
}
free(to_delete);
}
else {
previous = current;
current = current->next;
}
}
return first;
}

Binary file not shown.