Hello
How do I deal with Trees as Linked Lists ??

View Photos,

[IMG]http://img832.imageshack.us/img832/4553/49362381.png[/IMG]

Uploaded with ImageShack.us


I think I would need for TL* son ,TL* father, TL* brother.

struct ff
{
        int id;
        ff * son;
        ff * father;
        ff * brother;
};
class formula_s
{
        public:
            ~formula_s();
            formula_s();
            void add_formula(ff *p1);
};

I used to enter data this way,

f = new formula;
		f->id = 1;
		g = new formula;
		g->id = 11;
		f->son = g;
		g->father = f;
		p = new formula;
		p->id = 12;
		g->brother = p;
		p->father = g;
		p->brother = NULL;
		q = new formula;
		q->id = 13;
		p->son = q;
		q->father = p;
		p = new formula;
		p->id = 14;
		q->brother = p;
		p->father = q;
		p->brother = NULL;
		p = new formula;
		p->id = 15;
		g->son = p;
		p->father = g;
		p->brother = NULL;

I want to create a function >>> void add_formula(ff *p1);
so as to the user can enter data.

It looks like you want each node to point to the two below it, and the one above it.

In your code, you have

f = new formula;		
f->id = 1;		
f->op = 0;		
f->oe = 0;

This is a syntax error. You need to declare f as a struct ff, then you need to declare it as a new ff like so

ff f;
f = new ff;
// or
ff f = new ff;

Once you have done that, you can then assign id a value. What I'm not sure of is, what is f->op and f->oe? is there a formula class not listed but declared?

If you want the user to enter data, then call the function and cin that info.

void add_formula(ff *p1){
int data;
cout <<"Enter Data: ";
cin >> data;
p1->id = data;
}

See all the code.

#include <iostream>

using namespace std;

struct ff
{
        int id;
        ff * son;
        ff * father;
        ff * brother;
};
class formula_s
{
        public:
            ~formula_s();
            formula_s();
            void add_formula(ff *p1);  // I want to create this function...
};

int main()
{
	ff *f, *g, *p, *q;
	
		f = new ff;
		f->id = 1;
		g = new ff;
		g->id = 11;
		f->son = g;
		g->father = f;
		p = new ff;
		p->id = 12;
		g->brother = p;
		p->father = g;
		p->brother = NULL;
		q = new ff;
		q->id = 13;
		p->son = q;
		q->father = p;
		p = new ff;
		p->id = 14;
		q->brother = p;
		p->father = q;
		p->brother = NULL;
		p = new ff;
		p->id = 15;
		g->son = p;
		p->father = g;
		p->brother = NULL;



    int xx;
    cin >> xx; 
 	return 0;
}

I want to create a function >>> void add_formula(ff *p1);

Looks like you did all your work in main. What exactly do you want void add_formula (ff *p1) to do? I posted a workable add_formula function above for the class that will allow the user to enter the id for each person.

Looks like you did all your work in main. What exactly do you want void add_formula (ff *p1) to do? I posted a workable add_formula function above for the class that will allow the user to enter the id for each person.

I want create a function add_formula (ff *p1),Instead of this primitive method .

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.