Plugging the memory lead

This commit is contained in:
Anton Dolozin 2025-10-28 20:49:53 +01:00
parent 116569e226
commit b0c719ef34

View File

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