What am I doing wrong here? (Pointers and references)

I'm not sure how to correctly do this. This is just for practice and serves no real purpose. That's why I didn't make a class.

I'm basically creating a linked list using recursion here or the left side of a tree without all the rest of the implementation for now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

struct Node{
    int data;
    Node * left;
};

Node * root = nullptr;

void insert(Node** t, int x)
{
    Node **current = t;

    Node * n = new Node;
    n->data = x;
    n->left = nullptr;

    if(*current == nullptr)
    {
        *current = n;
    }
    else
    {
        if(x < (*current)->data)
        {
            (*current) = (*current)->left;
            insert(current, x);
        }
    }
}

void print()
{
    Node * current = root;
    cout << current->data << endl;
}

int main()
{
    insert(&root, 5);
    insert(&root, 4);
    insert(&root, 3);

    print();
}
Last edited on
Well, where do you set left to anything other than nullptr?

Actually, you need to link the node in the else branch of if(x < (*current)->data).

Line 28: You modify the root pointer.
So I got it thanks to your feedback. And I know my print function is terrible but its quick this way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

struct Node{
    int data;
    Node * left;
};

Node * root = nullptr;

void insert(Node** t, int x)
{

    Node * n = new Node;
    n->data = x;
    n->left = nullptr;

    if(*t == nullptr)
    {
        *t = n;
    }
    else
    {
        if(x < (*t)->data)
        {
            insert(&((*t)->left), x);
        }
    }
}

void print()
{
    Node * current = root;
    cout << current->data << endl;
    cout << current->left->data << endl;
    cout << current->left->left->data;
}

int main()
{
    insert(&root, 5);
    insert(&root, 4);
    insert(&root, 3);

    print();
}


I get passing by references but can someone explain this line because double pointers still confuse me.

 
insert(&((*t)->left), x);


How would this look on paper?

How is this read by the compiler? Does it look at the first * then the next * then the reference and then the dot operator?

And what is each pointer pointing to.

Last edited on
The idea of a double pointer is you are passing a pointer and the type which is points to is a pointer, which means you can modify the latter pointer from within the function.
You code would be unravelled one step at a time
1
2
3
4
insert(&((*t)->left),x);
insert(&(Object-Point-To-by-T->left),x);
insert(&Member-Left-Of-Object-Pointed-To-By-Object-Pointed-To-By-t,x);
insert(Address-Of-Member-Left-Of-Object-Pointed-To-By-Object-Pointed-To-By-t,x);
Thank you shadow. That helped a lot.
Topic archived. No new replies allowed.