As I said in the last thread, the problem is that you are looking at the address of the pointer variables, rather than the value of the pointer variables (i.e. where the variables are pointing). You also don't want to update *list unless the entry being deleted is the first one. Your calling function is relying on textfile to point to the start of the list.
Here is an updated version of delete_node_debug based on the code I offered on the last thread. I just used victim
instead of *list
in the loop, which leaves *list
pointing to the start of the list. Then at the end, you can check if you are removing the start of the list if victim == *list
void
delete_node_debug (rec **list, int node)
{
// test that list exists
if (!*list) {
fprintf (stdout,"%s(), The list is empty\n",__func__);
return;
}
// get size of list
int szlist = getszlist (*list);
// test node < szlist
if (node >= szlist || node < 0) {
fprintf (stderr, "%s(), Error: record to delete is out of range (%d)\n", __func__, node);
return;
}
rec *victim = *list;
// find the node'th node with balanced search
// search fwd for 0->szlist/2, rev end->szlist/2
if (node != 0 && node >= szlist/2) {
while (szlist - node++)
victim = victim->prev;
fprintf (stderr, "\nlist pointer state after reverse iteration to node: %d\n",
victim->lineno);
psurround (&victim);
} else
while (node--)
victim = victim->next;
// non-self-reference node means just rewire
if (victim != victim->next) {
(victim->prev)->next = victim->next;
(victim->next)->prev = victim->prev;
// If we are deleting the first item, then we need to change the passed in pointer
if (victim == *list)
*list = victim->next;
} else { // deleted node was self-referenced. last node
*list = NULL;
}
free (victim); // delete the node
}
Also, change your debugging output function psurround to print out the pointer values, rather than the addresses of the pointer variables:
void
psurround (rec **list) {
fprintf (stderr, "%2d - prev: %p cur: %p next: %p\n",
(*list)->prev->lineno,
(*list)->prev->prev,
(*list)->prev,
(*list)->prev->next);
fprintf (stderr, "%2d - prev: %p cur: %p next: %p\n",
(*list)->lineno, (*list)->prev, *list, (*list)->next);
fprintf (stderr, "%2d - prev: %p cur: %p next: %p\n\n",
(*list)->next->lineno,
(*list)->next->prev,
(*list)->next,
(*list)->next->next);
}
In general, I think any time you have multiple &s and *s in the same term (like *&(*list)), you need to rethink what is going on.