Actually, it's worse than that.
Then why does it work?
Code:
void DeleteNode(struct Node *pNode)
{
if(pNode->pPrev) pNode->pPrev->pNext = pNode->pNext;
if(pNode->pNext) pNode->pNext->pPrev = pNode->pPrev;
free(pNode);
}
void EmptyList()
{
while(pRoot->pNext) DeleteNode(pRoot->pNext);
}
/// here
EmptyList();
free(pRoot);
Okay, lets go through this step by step:
Code:
-> -> -> ->
Root Element1 Element2 Element3 NULL
<- <- <-
Code:
while(pRoot->pNext) // TRUE
DeleteNode( pRoot->pNext ) // pNode = &Element1
if(pNode->pPrev) // TRUE
pNode->pPrev->pNext = pNode->pNext
Code:
--------------> -> ->
Root Element1---> Element2 Element3 NULL
<- <- <-
Code:
if(pNode->pNext) // TRUE
pNode->pNext->pPrev = pNode->pPrev
Code:
--------------> -> ->
Root <---Element1---> Element2 Element3 NULL
<-------------- <-
Code:
-> -> ->
Root Element2 Element3 NULL
<- <-
Get the idea?