Problems with struct?

How do i make a struct which points itself?
and also contains a pointers 2 other stuct of the same struct. ??

1
2
3
4
5
6
7
8
9
10
struct point;
{
    int x;
    int y;
    struct point *p1;  //pointer to another structure of the same structure
    struct point *p2;  //same here

   /*personally don't like writing "struct point" coz it seems too long.
       so i make an alias with  typdef  */
}
personally don't like writing "struct point" coz it seems too long.
Where did you get that you have to write struct everytime?
1
2
3
4
5
stuct foo
{
    foo* next;
    foo* prev;
};
Well.. i somehow have to assign point to itself... i was thinking if it was possible to do it in the constructor.. ? or ??
somehow have to assign point to itself
You mean you need a pointer pointing to the current object? In that case use this pointer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct foo
{
    void print_me()
    {
        std::cout << this << '\n';
    }
};

int main()
{
    foo a;
    std::cout << &a << '\n';
    a.print_me();
}
0xbfc6aa6d
0xbfc6aa6d
http://ideone.com/n0UvSP
Ok.. so "this" seems useful in this situation.
how would i use the constructor from the struct as a parameter for class constructor. ??



how would i use the constructor from the struct as a parameter for class constructor. ??
Explain clearer what you want and why would you want it. To clear some things: structs are classes. Only difference between class declared with word class and class declared as struct is default access and inheritance modifiers: for class it is private, for struct: public
i have to design a binary tree structure, for which i was planingen on using struct to indicate each nodes, and a have a class which should be the tree structure itself.

So to instantiate the tree i was thinking of having the contructor for the tree class to take the struct as parameter, and thereby declaring the root as being that struct.

struct nodes{
int value;
nodes* itself;
nodes* right;
nodes* left;
nodes(int a)
{
value = a;
itself = this;
right = NULL;
left = NULL;
};
};

And then within that class have functions for inserting, deletion and so on,
Last edited on
Well, I see no problem with that. By why do you need itself member? Outside of a class you can use & operator, inside you can use this directly.
I am not quite sure where you are heading at my constructor for the class look like this
tree::tree(nodes a(int w )){
cout << a.value << a.itself << a.right << a.left << endl;

}

Which doesn't seem to work at the moment.. I don't se why i can't give the parameter like this..
something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class tree
{
    nodes a;
    tree(nodes a_) : a(a_)
    {
        cout << a.value << &a << a.right << a.left << endl;
    }
};

int main()
{
    nodes a(1);
    tree b(a);

    tree x(nodes(2));
}
Last edited on
I would like to keep them separated, but the constructor in class has to run the constructor for the struct aswell..

The idea is that the tree constructor has to have the constructor for the struct as parameter, such that, an struct object will be created with the wanted values, and these will be placed as the root for the tree in the tree structure..

Does it make sense??

So something like this

1
2
3
4
5
tree::tree //constructor for tree(nodes (a) //constructor for node) 
{
    cout << a.value << a.itself << a.right << a.left << endl;
    
}

so...
1
2
3
4
5
6
7
8
9
10
class tree 
{
    nodes a;
    tree(int);
};

tree::tree(int i) : a(i)
{
    cout << a.value << &a << a.right << a.left << endl;
}
or
1
2
3
4
5
6
7
8
9
10
class tree 
{
    nodes* a;
    tree(int);
};

tree::tree(int i) : a(new nodes(i))
{
    cout << a.value << &a << a.right << a.left << endl;
}
This was the method i was looking for

1
2
3
4
5
6
7
8
tree::tree(int i) : a(new nodes(i))
{
    cout << "Value of the tree " << a -> value << endl <<  "memory location " << a -> itself << endl << "pointer to right struct" <<  a ->right << endl <<"pointer to left struct "<<  a -> left << endl;
    
    nodes *p = a->itself;
    cout << "ds" << p ->  value << endl; // test if the pointer works 
    
}
Please not that you can (and should) use &a instead of making class store pointer to itself and waste memory.
Now i think about it.. Couldn't the tree easily be implemented as an double linked list?
Tree is a tree, not a list. Those are completely different structures, have different perfomance and uses.
I've tried implementing it as an double linked list, where instead of pointing to the next node, it points to both the left and the right node.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct node{
    int value;
    node* right;
    node* left;
};


class avl_tree{
public:
    avl_tree(int);
    node *a;
    node *root;
    void insert(int);
};


I am having some problems with my insert routine..
For some reason it overrides pointers example.

int main(int argc, const char * argv[]) {

avl_tree tree(6);
tree.insert(7);
tree.insert(5);
tree.insert(6);
tree.insert(4);
node *a = tree.root->right;
cout << a->value << endl;
std::cout << "Hello, World!\n";
return 0;
}

Should in my head create a tree structure being
http://snag.gy/eqTHJ.jpg

but instead tree.root->right doesn't have any value..
Last edited on
Topic archived. No new replies allowed.