Hi, in standard C, I'm trying to use my redblack tree code along with other data structures for my project. I currently have everything working (as far as I can test atleast) except the red-black tree. The red-black tree is in its own file redblacktree.c and main is in its own file main.c along with a few other files that don't affect the RB tree.
Here is the code.
rbtreeDriver(...)
struct rbTree* T = (struct rbTree*)malloc(sizeof(struct rbTree));
struct rbNode* nil = (struct rbNode*)malloc(sizeof(struct rbNode));
T->root = (struct rbNode*)malloc(sizeof(struct rbNode));
T->nil = (struct rbNode*)malloc(sizeof(struct rbNode));
struct rbNode* xPrint = (struct rbNode*)malloc(sizeof(struct rbNode));
int makeTree = 1;
if (makeTree == 1) {
//sets up the root and nil nodes for the tree
T->root = T->nil;
T->nil->color = 0;
T->nil->rc = nil;
T->nil->lc = nil;
T->nil->key = -1;
makeTree = 0
}
....return;
So my main.c calls the rbtreeDriver which creates the tree and works like its supposed to except after it returns to main so when its called again it recreates the tree which deletes all data thats currently stored.
So my question is is there a way I can have the program not declare the variables in rbTreeDriver(the function above) or is there a way that I could easily move all of the code into a separate function thats only called once (such as main) but still be able to pass the tree pointer "T" as needed without losing the data?
Any help would be very appreciated as I only have alil more than a day to get this final error knocked out.
Thank You
P.S. The redblack tree code works just as it should when I'm not trying to combine it with other files that call it as a function.