Self Referential Structures
Last Updated :
11 Jul, 2025
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;
}
Java
// Define the 'Node' class
class Node {
// Data members to store the data
int data1;
int data2;
// Reference to the next node
Node link;
// Constructor to initialize the data members
public Node(int data1, int data2)
{
this.data1 = data1;
this.data2 = data2;
this.link = null;
}
// Default constructor
public Node()
{
this.data1 = 0;
this.data2 = 0;
this.link = null;
}
}
// Main class to demonstrate the creation of Node instance
public class Main {
public static void main(String[] args)
{
// Create an instance of the 'Node' class using the
// default constructor
Node ob = new Node();
// Optionally, you can print the node's data to
// verify
System.out.println("Data1: " + ob.data1
+ ", Data2: " + ob.data2);
}
}
Python
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
- Self Referential Structure with Single Link
- 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 <stdio.h>
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
Python
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);
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 <stdio.h>
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;
}
Java
public class Main {
public static void main(String[] args)
{
// Create nodes
Node ob1 = new Node(); // Node1
Node ob2 = new Node(); // Node2
Node ob3 = new Node(); // Node3
// Initialize data for each node
ob1.data = 10;
ob2.data = 20;
ob3.data = 30;
// Set forward links
ob1.next_link = ob2;
ob2.next_link = ob3;
// Set backward links
ob2.prev_link = ob1;
ob3.prev_link = ob2;
// Accessing data of ob1, ob2 and ob3 by ob1
System.out.println(ob1.data + "\t"
+ ob1.next_link.data + "\t"
+ ob1.next_link.next_link.data);
// Accessing data of ob1, ob2 and ob3 by ob2
System.out.println(ob2.prev_link.data + "\t"
+ ob2.data + "\t"
+ ob2.next_link.data);
// Accessing data of ob1, ob2 and ob3 by ob3
System.out.println(ob3.prev_link.prev_link.data
+ "\t" + ob3.prev_link.data
+ "\t" + ob3.data);
}
}
class Node {
int data;
Node prev_link;
Node next_link;
}
Python
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)
JavaScript
class Node {
constructor(data) {
this.data = data;
this.prev_link = null;
this.next_link = null;
}
}
function main() {
// Create nodes
let ob1 = new Node(); // Node1
let ob2 = new Node(); // Node2
let ob3 = new Node(); // Node3
// Initialize data for each node
ob1.data = 10;
ob2.data = 20;
ob3.data = 30;
// Set forward links
ob1.next_link = ob2;
ob2.next_link = ob3;
// Set backward links
ob2.prev_link = ob1;
ob3.prev_link = ob2;
// Accessing data of ob1, ob2, and ob3 by ob1
console.log(ob1.data + "\t" + ob1.next_link.data + "\t" + ob1.next_link.next_link.data);
// Accessing data of ob1, ob2, and ob3 by ob2
console.log(ob2.prev_link.data + "\t" + ob2.data + "\t" + ob2.next_link.data);
// Accessing data of ob1, ob2, and ob3 by ob3
console.log(ob3.prev_link.prev_link.data + "\t" + ob3.prev_link.data + "\t" + ob3.data);
}
// Execute the main function
main();
Output10 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:
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem