So i’m having trouble figuring how to overcome this.
Take for example, i have a red black tree implementation that works with items:
typedef unsigned long int Key;
struct rbt_node{
Item item;
int color;
Key key;
struct rbt_node* parent;
struct rbt_node* left;
struct rbt_node* right;
};
then in an Item.h i define the structure i’ll be using, for example:
typedef struct _something* Item;
That way i’m decoupling the Item holded from the tree implementation. The problem arises if i want to reuse the ADT for other types.
At the moment i would have to define a Item2.h, and copy rbt.c/rbt.h to rbt2.c/rbt2.h and change them to use Item2.h and change the functions names. Isn’t there any cleaner way?
I found this C double linked list with abstract data type, but it seems to have some problems depending on the architecture and size of the structures, which i’m not really very knowledgeable.
I’m looking for this kind of usage:
rbt_insert(rbt_of_something, something);
rbt_insert(rbt_of_somethingElse, somethingElse);
Thanks
Make the item member
void*. Then define functions or macros that you use to assign/read the items to perform the required casts. e.g.If you want to be really clever and you have a fixed number of types you’d like to put in there, add an enum to rbt_node or rbt_tree to store the type of the item.