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* 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;
}