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
- Difference between Linear and Non-linear Data Structures
- Why Data Structures and Algorithms Are Important to Learn?
- Convert Adjacency Matrix to Adjacency List representation of Graph
- Duplicates Removal in Array using BST
- Find the element having maximum premutiples in the array
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Path with smallest product of edges with weight >= 1
- Find distinct integers for a triplet with given product
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.
Improved By : shubham_singh