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.
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.
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.
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.
Self referential structures are very useful in creation of other complex data structures like:
- Data Structures | Misc | Question 5
- How can one become good at Data structures and Algorithms easily?
- Difference between Stack and Queue Data Structures
- Data Structures and Algorithms Online Courses : Free and Paid
- Why companies like Amazon, Microsoft, Google focuses on Data Structures and Algorithms : Answered
- Introduction to Data Structures | 10 most commonly used Data Structures
- Write a function to get the intersection point of two Linked Lists | Set 2
- Minimum distance to visit all the nodes of an undirected weighted tree
- Sorting Algorithms Visualization | Selection Sort
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
- Count of triplets that satisfy the given equation
- Van Emde Boas Tree | Set 4 | Deletion
- Insertion in n-ary tree in given order and Level order traversal
- Red-Black Trees | Top-Down Insertion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.