Plugging the memory lead
This commit is contained in:
parent
116569e226
commit
b0c719ef34
@ -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;
|
||||||
|
|
||||||
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->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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user