When we need Self-referential structure? Discuss its implementation

10 c]When we need Self-referential structure? Discuss its implementation.

Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.

structures pointing to the same type of structures are self-referential in nature.

Lightbox

Example:

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

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.

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.

Lightbox

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.

Lightbox

Leave a Reply

Your email address will not be published. Required fields are marked *