Help free structs and pointer.
Hey guys I'm having troubles understanding how to free a list of linked structs.
For simplicity reason we'll say that my struct has a value and a ptr to the next item. Here is waht I have but when I compile it, it doesn' t do anything.
Code:
void removePtr(LIST_ELEM *pHead, int loc){
LIST_ELEM *ptr;
LIST_ELEM *tmpPtr;
int size = sizeOf(pHead);
int i = 0;
for(ptr = pHead; pHead != NULL; ptr++){
if( loc == 0){
tmpPtr = pHead;
pHead = ptr->pNext;
free(tmpPtr);
}
else if(loc = size-1){
LIST_ELEM *ptrB4 = nodeB4(pHead,loc);
ptrB4->pNext = NULL;
free(ptr);
}
else if( i == loc){
LIST_ELEM *ptrB4 = nodeB4(pHead,loc);
tmpPtr = ptr;
LIST_ELEM *ptrAfter = (tmpPtr->pNext);
ptrB4->pNext = ptrAfter;
free(tmpPtr);
}
i++;
}
}
remove takes in the head of the list and the location/index where we want the item removed.
nodeB4 returns the item that come before the item we want to remove. Any ideas?