Fixing the memory lead

This commit is contained in:
Anton Dolozin 2025-10-28 20:47:58 +01:00
parent 0f9e18cdd0
commit 116569e226

View File

@ -42,45 +42,24 @@ void cancel_train(struct car* first) {
struct car* clear_train(struct car* first, const char* target) { struct car* clear_train(struct car* first, const char* target) {
if(first == NULL){ struct car* curr = first;
return NULL; struct car* prev = 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;
} while (curr) {
struct car* prev= NULL; if (strcmp(curr->value, target) == 0) {
struct car* temp = first; struct car* to_delete = curr;
while (temp->next) if (prev)
{ prev->next = curr->next;
prev = temp; else
temp = temp->next; first = curr->next;
if(strcmp(temp->value, target) == 0){ curr = curr->next;
struct car* targetNode = temp; free(to_delete);
prev->next = temp->next; } else {
temp = temp->next; prev = curr;
free(targetNode); curr = curr->next;
}
}
else{
prev = temp;
temp = temp->next;
} }
} return first;
return first;
} }