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.

Why don't you create an instance or a mixed structure
I think you have to modify your structure like OOP type.
Here's my sample

struct rbtree {
         .rbtmember
         .rbtmember
};

struct makeTree {
       struct rbtTree NewRbtTree
       .memberTree
       .memberTree
} myinstance;

/* call the member */
myinstance.NewRbtTree.rbtMember
myinstance.memberTree



/* or you can put default value on that instance */
struct makeTree {
       struct rbtTree NewRbtTree
       .memberTree
       .memberTree
} myinstance = {{"rbt member", "rbt member" } "value1", "value2" };

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.

Hmm Ok. I've never done anything like that so I'm not quite sure on exactly how to change everythign my struct currently looks like this.

struct node
{
	int key;
	int color; //red = 1 black = 0
	struct node *rc;
	struct node *lc;
	struct node *p;
};
//struct for the tree
struct rbTree
{
	struct node *root;
	struct node *nil;
};

hope the following will help you a lot.

this is how to use Pointers as Structure Members

struct data {
     int *value;
     int *rate;
} first; /*this is the instance*/

how to use?

first.value = &cost;   /*first.value the initialize to &cost*/
first.rate = &interest; /*first.rate */

struct msg {
   char *p1;
   char *p2;
} myptrs; /* this is the instance */

myptrs.p1 = "the boy";
myptrs.p2 = "the girl";

/* run pointers with array as type char as structure.

struct msg {
      char p1[30];
      char *p2;
} myptrs;

strcpy(myptrs.p1, "the boy");
strcpy(mypts.p2, "the girl");
puts(myptrs.p1);
puts(myptrs.p2);

/* use pointers to structure */

struct customer {
          int id;
          char name[10];
};

Now declare a pointer to type customer

struct part *p_part;

struct part sales; /*declare another struct*/

p_part=&sales; /*Perform the pointer initialization*/

/*calling the member */
sales.number
sales.name[]

or

(*p_part).number = 100; /*      *p_part refers to "sales" */

or using membership operator "->"

p_part -> number
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.