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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Difference between Linear and Non-linear Data Structures
- Why Data Structures and Algorithms Are Important to Learn?
- Static and Dynamic data structures in Java with Examples
- Real-time application of Data Structures
- Specific Level Order Traversal of Binary Tree
- Median of sliding window in an array | Set 2
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Find the node at the centre of an N-ary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : shubham_singh