# Self Referential Structures

Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member. In other words, structures pointing to the same type of structures are self-referential in nature

Example:

## CPP

 `struct` `node {` `    ``int` `data1;` `    ``char` `data2;` `    ``struct` `node* link;` `};`   `int` `main()` `{` `    ``struct` `node ob;` `    ``return` `0;` `}`

## Python3

 `class` `node:` `    ``def` `__init__(``self``):` `        ``self``.data1``=``0` `        ``self``.data2``=``''` `        ``self``.link``=``None`   `if` `__name__ ``=``=` `'__main__'``:` `    ``ob``=``node()`

## Javascript

 `// Define the 'node' object` `class node {` `    ``constructor(data1, data2) {` `        ``this``.data1 = data1;` `        ``this``.data2 = data2;` `        ``this``.link = ``null``;` `    ``}` `}` `// Create an instance of the 'Node' object` `let ob = ``new` `Node();`

In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it contains garbage value.

Types of Self Referential Structures

1. Self Referential Structure with Single Link
2. Self Referential Structure with Multiple Links

Self Referential Structure with Single Link: These structures can have only one self-pointer as their member. The following example will show us how to connect the objects of a self-referential structure with the single link and access the corresponding data members. The connection formed is shown in the following figure. Implementation:

## C++

 `#include `   `struct` `node {` `    ``int` `data1;` `    ``char` `data2;` `    ``struct` `node* link;` `};`   `int` `main()` `{` `    ``struct` `node ob1; ``// Node1`   `    ``// Initialization` `    ``ob1.link = NULL;` `    ``ob1.data1 = 10;` `    ``ob1.data2 = 20;`   `    ``struct` `node ob2; ``// Node2`   `    ``// Initialization` `    ``ob2.link = NULL;` `    ``ob2.data1 = 30;` `    ``ob2.data2 = 40;`   `    ``// Linking ob1 and ob2` `    ``ob1.link = &ob2;`   `    ``// Accessing data members of  ob2 using ob1` `    ``printf``(``"%d"``, ob1.link->data1);` `    ``printf``(``"\n%d"``, ob1.link->data2);` `    ``return` `0;` `}`

## Java

 `// java implementation of above approach` `public` `class` `Main {` `    ``static` `class` `Node {` `        ``int` `data1;` `        ``int` `data2;` `        ``Node link;` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``Node ob1 = ``new` `Node(); ``// Node1`   `        ``// Initialization` `        ``ob1.link = ``null``;` `        ``ob1.data1 = ``10``;` `        ``ob1.data2 = ``20``;`   `        ``Node ob2 = ``new` `Node(); ``// Node2`   `        ``// Initialization` `        ``ob2.link = ``null``;` `        ``ob2.data1 = ``30``;` `        ``ob2.data2 = ``40``;`   `        ``// Linking ob1 and ob2` `        ``ob1.link = ob2;`   `        ``// Accessing data members of  ob2 using ob1` `        ``System.out.println(ob1.link.data1);` `        ``System.out.println(ob1.link.data2);` `    ``}` `}` `// This code is implemented by Chetan Bargal`

## Python3

 `class` `node:` `    ``def` `__init__(``self``):` `        ``self``.data1``=``0` `        ``self``.data2``=``0` `        ``self``.link``=``None`   `if` `__name__ ``=``=` `'__main__'``:` `    ``ob1``=``node() ``# Node1`   `    ``# Initialization` `    ``ob1.link ``=` `None` `    ``ob1.data1 ``=` `10` `    ``ob1.data2 ``=` `20`   `    ``ob2``=``node() ``# Node2`   `    ``# Initialization` `    ``ob2.link ``=` `None` `    ``ob2.data1 ``=` `30` `    ``ob2.data2 ``=` `40`   `    ``# Linking ob1 and ob2` `    ``ob1.link ``=` `ob2`   `    ``# Accessing data members of  ob2 using ob1` `    ``print``(ob1.link.data1)` `    ``print``(ob1.link.data2)`

## C#

 `using` `System;`   `public` `class` `MainClass {` `    ``public` `class` `Node {` `        ``public` `int` `data1;` `        ``public` `int` `data2;` `        ``public` `Node link;` `    ``}`   `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``Node ob1 = ``new` `Node(); ``// Node1`   `        ``// Initialization` `        ``ob1.link = ``null``;` `        ``ob1.data1 = 10;` `        ``ob1.data2 = 20;`   `        ``Node ob2 = ``new` `Node(); ``// Node2`   `        ``// Initialization` `        ``ob2.link = ``null``;` `        ``ob2.data1 = 30;` `        ``ob2.data2 = 40;`   `        ``// Linking ob1 and ob2` `        ``ob1.link = ob2;`   `        ``// Accessing data members of  ob2 using ob1` `        ``Console.WriteLine(ob1.link.data1);` `        ``Console.WriteLine(ob1.link.data2);` `    ``}` `}`

## Javascript

 `class node {` `  ``constructor() {` `    ``this``.data1 = 0;` `    ``this``.data2 = 0;` `    ``this``.link = ``null``;` `  ``}` `}`   `// Create node1` `let ob1 = ``new` `node();`   `// Initialization` `ob1.link = ``null``;` `ob1.data1 = 10;` `ob1.data2 = 20;`   `// Create node2` `let ob2 = ``new` `node();`   `// Initialization` `ob2.link = ``null``;` `ob2.data1 = 30;` `ob2.data2 = 40;`   `// Linking ob1 and ob2` `ob1.link = ob2;`   `// Accessing data members of ob2 using ob1` `console.log(ob1.link.data1);` `console.log(ob1.link.data2);`

Output

```30
40

```

Self Referential Structure with Multiple Links: Self referential structures with multiple links can have more than one self-pointers. Many complicated data structures can be easily constructed using these structures. Such structures can easily connect to more than one nodes at a time. The following example shows one such structure with more than one links.
The connections made in the above example can be understood using the following figure. Implementation:

## CPP

 `#include `   `struct` `node {` `    ``int` `data;` `    ``struct` `node* prev_link;` `    ``struct` `node* next_link;` `};`   `int` `main()` `{` `    ``struct` `node ob1; ``// Node1`   `    ``// Initialization` `    ``ob1.prev_link = NULL;` `    ``ob1.next_link = NULL;` `    ``ob1.data = 10;`   `    ``struct` `node ob2; ``// Node2`   `    ``// Initialization` `    ``ob2.prev_link = NULL;` `    ``ob2.next_link = NULL;` `    ``ob2.data = 20;`   `    ``struct` `node ob3; ``// Node3`   `    ``// Initialization` `    ``ob3.prev_link = NULL;` `    ``ob3.next_link = NULL;` `    ``ob3.data = 30;`   `    ``// Forward links` `    ``ob1.next_link = &ob2;` `    ``ob2.next_link = &ob3;`   `    ``// Backward links` `    ``ob2.prev_link = &ob1;` `    ``ob3.prev_link = &ob2;`   `    ``// Accessing  data of ob1, ob2 and ob3 by ob1` `    ``printf``(``"%d\t"``, ob1.data);` `    ``printf``(``"%d\t"``, ob1.next_link->data);` `    ``printf``(``"%d\n"``, ob1.next_link->next_link->data);`   `    ``// Accessing data of ob1, ob2 and ob3 by ob2` `    ``printf``(``"%d\t"``, ob2.prev_link->data);` `    ``printf``(``"%d\t"``, ob2.data);` `    ``printf``(``"%d\n"``, ob2.next_link->data);`   `    ``// Accessing data of ob1, ob2 and ob3 by ob3` `    ``printf``(``"%d\t"``, ob3.prev_link->prev_link->data);` `    ``printf``(``"%d\t"``, ob3.prev_link->data);` `    ``printf``(``"%d"``, ob3.data);` `    ``return` `0;` `}`

## Python3

 `class` `node:` `    ``def` `__init__(``self``):` `        ``self``.data``=``0` `        ``self``.prev_link``=``None` `        ``self``.next_link``=``None`   `if` `__name__ ``=``=` `'__main__'``:` `    ``ob1``=``node() ``#Node1`   `    ``# Initialization` `    ``ob1.prev_link ``=` `None` `    ``ob1.next_link ``=` `None` `    ``ob1.data ``=` `10`   `    ``ob2``=``node() ``#Node2`   `    ``# Initialization` `    ``ob2.prev_link ``=` `None` `    ``ob2.next_link ``=` `None` `    ``ob2.data ``=` `20`   `    ``ob3``=``node() ``# Node3`   `    ``# Initialization` `    ``ob3.prev_link ``=` `None` `    ``ob3.next_link ``=` `None` `    ``ob3.data ``=` `30`   `    ``# Forward links` `    ``ob1.next_link ``=` `ob2` `    ``ob2.next_link ``=` `ob3`   `    ``# Backward links` `    ``ob2.prev_link ``=` `ob1` `    ``ob3.prev_link ``=` `ob2`   `    ``# Accessing  data of ob1, ob2 and ob3 by ob1` `    ``print``(ob1.data,end``=``'\t'``)` `    ``print``(ob1.next_link.data,end``=``'\t'``)` `    ``print``(ob1.next_link.next_link.data)`   `    ``# Accessing data of ob1, ob2 and ob3 by ob2` `    ``print``(ob2.prev_link.data,end``=``'\t'``)` `    ``print``(ob2.data,end``=``'\t'``)` `    ``print``(ob2.next_link.data)`   `    ``# Accessing data of ob1, ob2 and ob3 by ob3` `    ``print``(ob3.prev_link.prev_link.data,end``=``'\t'``)` `    ``print``(ob3.prev_link.data,end``=``'\t'``)` `    ``print``(ob3.data)`

Output

```10    20    30
10    20    30
10    20    30

```

In the above example we can see that ‘ob1’, ‘ob2’ and ‘ob3’ are three objects of the self referential structure ‘node’. And they are connected using their links in such a way that any of them can easily access each other’s data. This is the beauty of the self referential structures. The connections can be manipulated according to the requirements of the programmer.

Applications: Self-referential structures are very useful in creation of other complex data structures like:

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next