nested class / struct that contains same-type pointer

i am struggling to grasp something technical. if a struct or class has a nested pointer to it's same type, then that pointer also points to another identicle copy of the object, right? but if the pointer points to another identicle copy then that other copy also has the same-type pointer and thus it just seems that there should be an infinite decleration of the nested pointers pointing to exact copies.

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
#include <iostream>
using namespace std;

struct node
{
    int x;
    struct node * next;
};

int main()
{
    node pik; // instantiate a structure
    pik.x = 5; // assign a value to it's member.
    pik.next->x = 10; // assign value to different x that belongs to another struct?
    cout << pik.next->x << endl; // displays 10
    cout << pik.x << endl; // displays 5
    
    // so every node struct created also contains a (same type) node pointer.  in essence there are two structs declared by just instantiatig one of them, right?
    // if so then why isn't there an infinite amount of structs since the pointer points to a location that contains a struct and it's member objects as well?
    
    // i mean this line below will compile but will obviously crash.
    //pik.next->next->next->x = 5;

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

struct node
{
    int x;
    struct node * next;
};

int main()
{
   node pik1; //Create first node.
   pik1.x = 5;

   node pik2; //Create second node.
   pik2.x = 10;

   //pik1.next //Right now this points to what?  Whatever the compiler initialized it to, cannot use yet.
   pik1.next = &pik2;  //pik1.next now points to pik2, this is not circular.  pik2.next is still junk.
   pik2.next = &pik1; //Now this is circular, if you don't want that don't do that.
    return 0;
}


in essence there are two structs declared by just instantiatig one of them, right?

No, see my example.

Also note everything was on the stack, usually you will want to allocate memory on the heap because once the node leaves scope the next pointer will no longer point to valid object.
It contains a pointer to an object of the same type, but not to the same object. it's a mechanism which is widely used in recursive data structures such as linked lists and binary trees.

your node struct contains a pointer to a node, so typically it would point to another object in order to form a list (which in turn may point to another object, which points to another, etc).

A simple (and completely useless) implementation of a linked list might look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    node n1, n2, n3;
    n1.next = &n2;
    n2.next = &n3;
    n3.next = NULL;

    node* ptr = &n1;
    while( ptr != NULL )
    {
        std::cout << "Stepped to the next node" << std::endl;
        ptr = ptr->next;
    }
} 
The basic concept is pretty similar to the idea of having a pointer-to-a- pointer-to-a pointer-to-a- pointer-to-a- pointer-to-a- pointer-to-a- pointer-to-a- pointer-to-a- .... {etc.} With the difference being that each pointer also indicates the location of a bit of data as well. (Its use being an alternative to storing data contiguously in arrays)


Have a read of the data structures pages on the website linked below for more information about data structures (the concept of a binary tree builds on top of the concept of a linked list, so you might want to start by trying to understand linked lists first):

http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_linklist.aspx
http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_bst1.aspx
Last edited on
yes it is regarding a singly linked list. it's my first introduction to such a container. teaching myself with c++ primer plus. my brain is in a deep fryer as i type this.
Topic archived. No new replies allowed.