The title is pretty self explanatory. Here's the function I've written for this purpose:
void wipeLoneCells()
{
cell *tmp;
tail = head;
while (1)
{
if (head == tail && !tail->flag)
{
head = head->next;
free(tail);
tail = head;
continue;
}
tmp = tail->next;
/***/ if (tmp->next == NULL && !tmp->flag)
{
tail->next = NULL;
free(tmp);
break;
}
else if (!tmp->flag)
{
tail->next = tmp->next;
free(tmp);
continue;
}
tail = tail->next;
}
}
The list's head and tail are global, and the list is built by the time this function gets called with head pointing to the first node and tail pointing to the last (whose next is NULL). I'm almost certain that my linked list is built correctly as I can print them with no errors. Sometimes this function works perfectly and sometimes it results in an access violation at the line marked with stars. I know it's not completely wrong as I do get the result I want when it doesn't produce an error, although I do get the error frequently so there must be something I'm overlooking. Thank you in advance for any help.
EDIT: Here's the fixed code:
void wipeLoneCells()
{
cell *tmp;
tail = head;
while (1)
{
if (head == tail && !tail->flag)
{
head = head->next;
free(tail);
tail = head;
continue;
}
tmp = tail->next;
if (tmp->next == NULL && !tmp->flag)
{
tail->next = NULL;
free(tmp);
break;
}
else if (tmp->next == NULL)
{
tail = tmp;
break;
}
else if (!tmp->flag)
{
tail->next = tmp->next;
free(tmp);
continue;
}
tail = tail->next;
}
}