understanding pointers

So I've been playing around with linked list adding functions here and there just to test my conceptual understanding of them.

This is just a snippet of code from the list class I've made.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void List::push_back(int d)
{
    Node * n = new Node;
    n->next = 0;
    n->data = d;

    if(head != 0)
    {
        current = head;
        while(current->next != 0)
        {
            current = current->next;
        }
        current->next = n;
    }
    else{
        head = n;
    }

}


In this code, how exactly does this work.

 
n->next = 0;


to my understanding this is the same as this:

 
(*n).next = 0;


In my node class next is declared as follows:

 
Node * next;


I'm having a hard time wrapping my head around how this is working. What I believe is that since next is a pointer of type node, that next stores the address 0 inside next which is inside node n, right?
A pointer holds an address. Hopefully, what is at that address is an object of the desired type... so if you do things properly, a Node* pointer will always point to a Node object.

The notable exception is if the pointer contains a "null" address, or an address of 0. In which case it is assumed to point to "nothing". For a linked list, this is useful because you can check to see if a node is null to know whether or not there are any nodes after it in the list.

With that in mind:
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
void List::push_back(int d)
{
    Node * n = new Node;    // This creates a new unnamed node.  Let's call it "foo".
                            //   Our pointer 'n' now points to the 'foo' node.
    n->next = 0;            // Set foo's 'next' member to point to nothing, indicating that 'foo'
                            //   is to be the last node in our list.
    n->data = d;            // Set foo's data member to hold the appropriate data.

    // Now that we have a node prepped, we need to actually place it at the end of our existing list
    if(head != 0)       // if there are ANY nodes in the list
    {
        // Then find the end of the list.  This loop walks through the list until it finds
        //   the last node.  We know we found the last node once we find the null 'next' pointer.
        current = head;
        while(current->next != 0)
        {
            current = current->next;
        }
        
        // at this point, 'current' now points to the last node in the list.  We no longer
        //   was that to be the last node in the list... we want 'foo' to be the last node.
        //   So change the last node's 'next' pointer to point to foo:
        current->next = n;
        
        // to recap, where we once had this in our list:
        //      first -> second -> third -> 0
        //
        // 'third' would have been the last node in the list.  And above... 'current' would be pointing to third.
        //    But now that we've set third.next to point to foo, we now have:
        //      first -> second -> third -> foo -> 0
        //
        // Making 'foo' the new last node.
    }
    else    // else, if head is null (indicating there are no nodes in the list)
    {
        // simply make 'foo' the head node.
        head = n;
    }

}



In this code, how exactly does this work.

n->next = 0;

to my understanding this is the same as this:

(*n).next = 0;


You are correct. When you use the arrow operator (->) you are accessing whatever the pointer points to (in this case, 'foo') rather than accessing the pointer itself.
Thanks so much for that thorough explanation. Best one I've ever had anyone give me. I understand everything now.
Topic archived. No new replies allowed.