Hello,
I made what I believed to be a simple linked-list system a short while ago, but ever since I stopped coding it and started using it with things I have been patching holes in my logic ever since. Most recently, the code seems to work fine, but over extended periods of usage of the list more holes seem to appear...
Anyways, could people please take a look at this:-
Code:
typedef struct TAGlist {
unsigned long data;
struct TAGlist *prev;
struct TAGlist *next;
} list;
list *root = NULL;
list *NewListItem(list *list, list *parent, unsigned long data)
{
if (list == NULL)
{
list = malloc(sizeof(list));
list->data = data;
list->prev = parent;
list->next = NULL;
}
else
list->next = NewListItem(list->next, list, data);
return list;
}
int DeleteListItem(list *olditem)
{
if (olditem == NULL)
return FALSE;
if (olditem->prev != NULL)
{
if (olditem->next != NULL)
olditem->prev->next = olditem->next;
else
olditem->prev->next = NULL;
}
else if (olditem->next != NULL)
{
root = olditem->next;
root->prev = NULL;
}
else
root = NULL;
free(olditem);
olditem = NULL;
return TRUE;
}
list *GetListItem(list *list, unsigned long data)
{
if (list != NULL)
{
if (list->data == data)
return list;
else
return GetListItem(list->next, data);
}
return NULL;
}
...and tell me where in the code there might be some ambiguity? My last error stemmed from the fact that although an item was supposedly deleted and freed, it was somehow referred to later on in execution and its next member was accessed, obviously leading nowhere but a stalwart Access Violation.