The list is becoming invalid after a call to
groceryList::addAfter
when the first argument is
head
head
is the pointer to the start of your linked list.
head->prev
should always be
NULL
since the list is not circular.
head->next
is always the second node in the list, which might be
NULL
if there is one element. If there were zero elements
head
would be
NULL
,
NULL->anything
leads to catastrophe (null pointer runtime related errors).
For some reason it helps me to list laws that I know should hold true. (Probably because I like proofs, but I digress).
head
is crucial to the
class groceryList
.
Here is what I think the function might should look like based on what you have said so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void GroceryList::addAfter(ItemType *& link, ItemType *newNode)
{
if(link==NULL)
{ //if the link passed in is NULL, an error should probably be thrown
//or maybe the newNode should be added just before head
//but I am going to just assume that the list is empty
head=newNode;
tail=newNode;
newNode->prev=NULL;
newNode->next=NULL;
}
else
{
newNode->prev=link;
newNode->next=link->next;
link->next=newNode;
if(tail == link)
{
tail = newNode;
}
}
}
|
Edit: As a side note I would like to say that pointers passed by reference is a bit of a headache.