I'm trying to code a C program that convert a binary tree into a linket list. Here's the declaration of these to structures :
struct treeNode {
double data;
struct treeNode* leftP;
struct treeNode* rightP;
};
struct listNode{
double data;
struct listNode* nextP;
};
The algorithm works but I have waht I expect to be a memory error at the end of the program's execution (process returned 0xC0000005). After some search with this code, I guess it comes from a bad management of pointers but I can't find my mistake. Here's my main function :
int main()
{
/*Creation of the binary tree*/
struct treeNode tree[10];
int i;
for(i=0;i<10;i++)
tree[i].data = (float)i;
tree[0].leftP = &(tree[1]);
tree[0].rightP = &(tree[2]);
//....... (creation of the tree)
tree[9].rightP = 0;
//Conversion
void** elem = malloc(sizeof(void**));
*elem = (void*)(&tree);
tree2list(elem);
struct listNode* list = (struct listNode*)(*elem);
printList(list);
printf("end1");
//free(elem);
printf("end2\n");
return 0;
}
The program reach end2 if I comment the free line and end1 if I try to free the variable elem. Here's the tree2list function :
void tree2list(void** node)
{
struct treeNode* tree = (struct treeNode*)(*node); //tree to convert
int size = treeSize(tree);
struct listNode* list = malloc(size*sizeof(struct listNode*)); //Creation of a list with same number of nodes than in the tree
*node = (void*) list;
struct listNode* currentListNode = list;
struct treeNode* currentTreeNode = tree;
struct treeNode* nextNode;
struct listNode* old = 0;
struct treeNode* stack = 0; //Stack composed of treeNode linked by their leftP pointer
while(currentTreeNode)
{
if(currentTreeNode->leftP) //if left branch exists, add the current node to the stack and explore this left branch
{
nextNode = currentTreeNode->leftP;
stackPush(&stack, currentTreeNode);
currentTreeNode = nextNode;
}
else //if left branch doesn't exist, add the currentNode to the list
{
currentListNode->data = currentTreeNode->data;
currentListNode->nextP = 0;
if(old)
old->nextP = currentListNode;
old = currentListNode++;
if(currentTreeNode->rightP)
currentTreeNode = currentTreeNode->rightP;
else
currentTreeNode = stackPop(&stack);
}
}
}
This function of other function like stackPush and stackPop that seem to work well. Do someone see the source of error code ?
Thanks