can you explain why i should not use the code
There's no reason, if you want to use C++. It isn't legal C.
At a guess the function you've provided should be implemented like:
Code:
void removeFromList(struct PTRnode **root,struct datatype data)
{
struct PTRnode *tempnode; //temporary node to hold root information
struct PTRnode *TheSuccessor; //a pointer to the next node after the selected node
if(strcmpi(KEY, PRIM_KEY)<0)//check if key is less than prim_key
{
if((*root)->left!=NULL) //check if the left pointer is not equal to NULL
{
removeFromList(&(*root)->left, data); //call function and go left
}
}
else
{
if(strcmpi(KEY, PRIM_KEY)==0)//check if the two keys match
{
if((*root)->left==NULL) //check if the left pointer is equal to NULL
{
if((*root)->right==NULL) //check if the right pointer is equal to NULL
{
//node is an empty leaf
free(*root); //free the memory used by root
*root=NULL; //assign a value of NULL to root
}
else
{
//only a right pointer exists
tempnode = *root; //store root contents into tempnode
*root = (*root)->right; //move contents of root->right into root
free(tempnode); //free memory of tempnode
}
}
else
{
if((*root)->right==NULL) //check if the right pointer is equal to NULL
{
//only a left pointer exists
tempnode = *root; //store root contents into tempnode
*root=(*root)->left; //move contents of root->left into root
free(tempnode); //free memory of tempnode
}
else
{
//both left and right pointers are not equal to NULL
//call to inordertraversal function that will check the next
//node and store the contents into TheSuccessor
TheSuccessor = deleteOrderTraversal(&(*root)->right, data);
(*root)->data = TheSuccessor->data; //store the information in thesuccessor
//into root
removeFromList(&(*root)->right, TheSuccessor->data); //call function and remove
//the successor of the node
}
}
}
else
{
if(strcmpi(KEY, PRIM_KEY)>0)//check if key is bigger than prim_key
{
if((*root)->right!=NULL) //check if the right pointer is not equal to NULL
{
removeFromList(&(*root)->right, data); //call function and go right
}
}
}
}
}
and you'd call it like:
int main(void)
{
struct PTRnode *root;
struct datatype data;
.
.
.
removeFromList(&root,data);
return 0;
}