Self Referential Structure in Data Structure

Self Referential Structure in Data Structure

In this tutorial, you are going to learn about Self Referential Structure in Data Structure and programming examples related to it.

Self Referential Structure

Self referential structures are those structures in which one or more pointers points to the structure of the same type.

Example:

struct self{
int p;
struct self *ptr;
};

Here struct self is the structure which consists of two members. One is the integer variable p and other one is struct self *ptr. Now here *ptr is the pointer to struct self itself. That is why it is called self referential structure because here we have one or more pointers pointing to the structure of the same type. Pointer ptr is pointing to the structure of the same type struct self.

Example of Self Referential Structure

struct code
{
int i;
char c;
struct code *ptr;
};
int main()
{
struct code var1;
strcut code var2;
var1.i= 55;
var1.c= 'A';
var1.ptr= NULL;
var2.i= 56;
var2.c= 'B';
var2.ptr= NULL;
var1.ptr= &var2;
printf("%d %c", var1.ptr->i, var1.ptr->c);
}

OUTPUT: 56B

Visualization

Self Referential Structure in Data Structure

This article on Self Referential Structure in Data Structure is contributed by Rajnish Kumar. If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Post a Comment

Previous Post Next Post

Contact Form