Have you ever wondered how data structures like trees and linked lists are implemented in C? All this is done by using self referential structures.
This article will show how self referential structure in C is defined and used with their practical applications. Whether you are a beginner in C or an expert, this article will help you understand the fundamentals of self-referential structure.
What is Self Referential Structure in C?
A Self referential structure is a structure that contains a member that points to the same type of structure. It is also called linked structure or recursive structure. Self referential structure in C is handy for creating complex data structures like linked lists, graphs and trees.
Syntax of Referential Structure in C is:-
The syntax of referential structure in C is:-
struct node{
// any data variable
int data;
// struct node pointer storing next node address
struct node *link;
};
The struct contains two members in this above code, data and link. The link is a pointer pointing to the same struct node type. This makes the node a self referential structure.
Why do we require a Self-referential structure?
Self-referential structures, typically implemented using pointers, are essential in C for several reasons. They enable the creation of dynamic data structures like linked lists and trees, accommodating changing data sizes during program execution. These structures are vital for representing complex data with nested or recursive elements, such as binary trees.
Self-referential structures also optimize memory usage by allowing efficient representation of interconnected data, as each element points to others of the same type. Moreover, they provide flexibility in designing data structures that can adapt to different requirements, making them a fundamental concept in C programming.
Types of Self Referential Structure in C
There are two types of self referential structure in C
Linear Self Referential Structure: These structures create a linear data structure such as stacks, queues and linked lists. In this, each member points to the next member sequentially.
Non Linear Self Referential Structure: These structures create a non linear data structure, such as trees and graphs. In this, each member can point to many members.
Examples to understand each type of Self Referential Structure in C
In this section, we will understand the self referential structure in C with the help of examples
Example 1
Here is an example of a linked list, a linear self referential structure implemented using a self-referential structure.
void main() { // assign each node a null value to avoid any refrence error struct Node* head =NULL; struct Node* second_node = NULL; struct Node* third_node = NULL;
// defining three nodes head = (struct Node*)malloc(sizeof(struct Node)); second_node = (struct Node*)malloc(sizeof(struct Node)); third_node = (struct Node*)malloc(sizeof(struct Node));
head->data = 10; // assign data in first node head->next = second_node; // Link first node with second
second_node->data = 200; // assign data to second node second_node->next = third_node;
third_node->data = 3000; // assign data to third node third_node->next = NULL;
// calling the function to display value display(head); }
In the above code, we have created a struct node that contains data and a pointer named next to store next node address. We define 3 nodes here, assign them values, and use the display() to print the value of each node. In display(), the while condition will run till we encounter a null node.
Example 2
Here is an example of a binary tree, a non-linear self-referential structure implemented using a self referential structure.
In the above code, we have made a binary tree using createNode function which is used to create a new node and then we performs a inorder traversal of the tree. In the inorder function, the while condition will run until we get a null node.
So now, we shall discuss some important features of the Self Referential Structure in C.
Defining dynamic data structures: Self referential structures define dynamic data structures such as linked lists, trees, and graphs, where the structures can be modified at runtime.
Connecting nodes/structures: Self referential structures can also connect nodes in various data structures. In a linked list, we have a pointer variable next that stores the following node address or links consecutive nodes, and tree's parent root is connected to its children.
Representing relationships: Self-referential structures can also show relationships among different elements in a data structure. For example, in a binary tree, each node represents a parent-child relationship between nodes, while in graphs, we have adjacent node relationships with each other.
Iterating through a data structure: Self-referential structures can be used to iterate through data structures as we have the pointer which stores the address of the next node so we can iterate by following the pointers from one element to another. It is instrumental in searching and sorting algorithms and can also be used to traverse data structures like a linked list. Also read - Bit stuffing program in c
Frequently Asked Questions
Q1. What is an example of a self-referential statement?
"This statement is false" is a self-referential statement because it refers to and describes itself.
Q2. What is an example of a self-referential relationship?
In databases, a self-referential relationship occurs when a table has a foreign key that references its own primary key.
Q3. What is an example of self-referential literature?
"Miguel de Unamuno's 'Mist' where the author interacts with his characters, acknowledging the fictional nature of the narrative."
Q4. What is the self-referential structure?
In programming, a self-referential structure is a data structure like a linked list where each node contains a reference to another node of the same type.
Conclusion
This article has explained the fundamentals of self referential structure in C. It has also given examples of self referential structure implemented in code and discussed its applications.
We hope this blog has helped you. We recommend you visit our articles on different topics of C, such as