# Graphs in C++

Can someone please explain me what does this struct inside a struct mean???
I am just trying to learn graphs representation.

``````struct AdjListNode
{
int dest;
};
``````

In this structure , dest is of int type and next is a pointer which contains address of AdjListNode structure. I hope you know that pointer stores address not data.

For example
int d = 5 Here d is variable of int type. And suppose, d is stored at 0x1 memory address.

1. int ptr*
2. ptr = &d ,
Here ptr will contain 0x1 memory address. And will refer to d.

Similary , pointer â€śnextâ€ť will contain address of a AdjListNode structure and refer to it.

It is not struct inside a struct. The name of the structure is AdjListNode which has two fields, one is of int type and second one is a pointer.

1 Like

â€śnextâ€ť is pointer to structure â€śAdjListNodeâ€ť.As pointers are used to store address of the variable.Like (intx)
x will be used to store memory address of a variable of int type similarly,next will be storing address of structure.so the line struct AdjListNode
next; stating that next is a pointer to structureâ€¦Hope i was clear.

Hello,

As others pointed out correctly, this is not a structure inside a structure, itâ€™s instead a very simple example of what is called a self-referenced data structure because itâ€™s a data structure defined in terms of â€śitselfâ€ť.

In fact, if you think on a simple linked list as being composed of simple blocks which contain two fields:

• Useful information;
• Pointer to the next block;

you will see that defining such data structure â€śrecursivelyâ€ť is the most intuitive way of doing thingsâ€¦ Namely:

``````struct AdjListNode {
int dest;
};
``````

is defining the node which will represent an element on an adjacency list (at least, by the name given) and it is composed of useful information (an int called dest) and support information (namely a pointer to the next element on the list).

Say you have this (in pseudocode):