Let us understand what is a self-referential structure in C programming and how do we implement a self referential structure in data structures.
Before we go ahead with self-referential data structures, let us also have a look at what are structures and in case you know it, you can skip it.
What are Structures?
An array is a collection of homogeneous elements but in the real world, we may need to include different types of logically related data.
To store different data such as name, age, contact, etc. associated with a single entity, we make use of structures.
A structure is, therefore, capable of storing heterogeneous data under a single name and the data elements are called as members.
What is a Self-Referential Structure?
A structure that contains pointers to a structure of its own type is known as self-referential structure.
In other words, a self-referential C structure is the one which includes a pointer to an instance of itself.

Syntax of Self-Referential Structure in C Programming
1 2 3 4 5 | struct demo { datatype member1, member2; struct demo *ptr1, *ptr2; } |
As you can see in the syntax, ptr1 and ptr2 are structure pointers that are pointing to the structure demo, so structure demo is a self referential structure. These types of data structures are helpful in implementing data structures like linked lists and trees.
It is an error to use a structure variable as a member of its own struct type structure or union type union, respectively.
Self Referential Structure Example
1 2 3 4 5 | struct node { int data; struct node *link; }; |
The concept of linked lists, stacks, queues, trees and many others works on the principle of self-referential structures.
One important point worth noting is that you cannot reference the typedef that you create within the structure itself in C programming.
Implementation of Self Referential Structure in Linked Lists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *link; }*front = NULL, *rear = NULL; int isEmpty() { if(front == NULL) { return 1; } else { return 0; } } void insert(int element) { struct node *t; t = (struct node *)malloc(sizeof(struct node)); if(t == NULL) { printf("Queue is not Allocated\n"); return; } t->info = element; t->link = NULL; if(front == NULL) { front = t; } else { rear->link = t; } rear = t; } void display() { struct node *ptr; if(isEmpty()) { printf("Empty\n"); return; } printf("Queue Elements:\n"); for(ptr = front; ptr != NULL; ptr = ptr->link) { printf("%d ", ptr->info); } printf("\n\n"); } int main() { int option, element; while(1) { printf("1. Insert an Element (Node) in the Queue\n"); printf("2. Display All Elements of the queue\n"); printf("3. Exit\n"); printf("Enter your option:\t"); scanf("%d", &option); switch(option) { case 1: printf("Enter the Element to Add in Queue:\t"); scanf("%d", &element); insert(element); break; case 2: display(); break; case 3: exit(1); default: printf("Please select an appropriate option\n"); } } return 0; } |
If you have any doubts or errors in the self referential data structure implementations, let us know about it in the comment section. Find more about it here.