I have this structure
typedef struct node
{
struct node *prev;
void *data;
struct node *next;
}NODE;
typedef struct head
{
unsigned int length;
struct node *first;
struct node *last;
}HEAD;
STATUS addNode(HEAD *head, NODE *newNode, int loc)
{
int i;
STATUS ret = SUCCESS;
NODE *curPtr;
if(head && newNode && loc && (loc <= (head->length)+1))
{
if(loc == 1)
{
newNode->prev = NULL;
newNode->next = head->first;
if(head->first)
head->first = newNode;
}
else
{
curPtr = head->first;
for(i=1; i<(loc-1); i++){
curPtr = curPtr->next;
}
newNode->prev = curPtr;
newNode->next = curPtr->next;
if(curPtr->next){
curPtr->next->prev = newNode;
}
curPtr->next = newNode;
}
head->length++;
}
else
ret = FAILURE;
return ret;
}
STATUS removeNode(HEAD *head,NODE *nodeToRemove)
{
STATUS ret = SUCCESS;
NODE *curPtr;
if(head && head->first)
{
curPtr = nodeToRemove->prev;
curPtr->next = nodeToRemove->next;
if(!(curPtr->next)){
curPtr->next = head->first;
}
head->length--;
}
else
ret = FAILURE;
return ret;
}
I know I am not calling free(node) in remove from list, this call is made somewhere else
my problem is, that sometimes on the line newNode->next = curPtr->next;
in the add node it falls for segmentation fault
can you please tell me the reason that it might happen ?