I am trying to do some functions to manage trees that receive has argument pointers to functions.
Item is declared like this:
Code:
typedef void * Item;
There is a structure called NODE:
Code:
typedef struct {
Item * item;
struct NODE * right;
struct NODE * left;
}NODE;
Until now the only successful thing i did was the declaration of the function pointer in the header file. The debug didn't show problems but I don't know if it is correct...
Code:
int (*less) (Item, Item);
Now the function that i want write:
Code:
void insert_node (NODE * tree, NODE * new_node, [here i want the less pointer]){
if (less (tree->Item, new_node->Item){
if (tree->right==NULL){
tree->right= new_node;
return;
}
else {
insert_node(tree->right, new_node, [less function pointer again]);
}
}
else{/*same thing to the other side of the tree*/}
}
The idea is to make generic functions to manage trees of any kind of Item (with the correct castings) and than make only functions to specify the kind of item. An example is a tree of words, and than one function like this:
Code:
int less_alphabetic (char * str1, char * str2){
if (strcmp (str1, str2) <0) return 1;
else return 0;
}