Self Refrential Data Structures
A self-referential data structure, also known as a recursive data structure, is a data structure in which each instance of the structure contains a reference to another instance of the same type. This self-referential property allows for the creation of complex and hierarchical structures. Common examples of self-referential data structures include linked lists, trees, and graphs.
Let's look at some common examples:
1. Linked List:
In a linked list, each node contains data and a reference (or a pointer) to the next node in the sequence. The last node typically has a reference set to NULL to indicate the end of the list.
struct Node {
int data;
struct Node* next;
};
In a binary tree, each node has at most two children, each of which is a node in the same type. Nodes are connected through left and right pointers.
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
3. Graph:
In a graph, vertices are connected by edges. An adjacency list representation uses self-referential structures to represent each vertex and its connections.
struct Vertex {
int data;
struct Edge* edges;
};
struct Edge {
struct Vertex* destination;
struct Edge* next;
};
Here, edges in the Vertex structure is a self-reference to the linked list of edges, and next in the Edge structure is a self-reference to the next edge.
4. Doubly Linked List
You can create a structure that refers to itself, allowing you to build more complex structures.
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
In this example, the structure Node has both next and prev pointers, creating a doubly-linked list.
Using self-referential structures allows for the creation of dynamic and flexible data structures that can represent hierarchical relationships between elements. However, it's crucial to manage memory properly, especially in languages like C where manual memory management is required, to avoid memory leaks and other issues.
Example:
Here's an example of a self-referential structure representing a node in a singly linked list:
struct Node {
int data; // Data of the node
struct Node *next; // Pointer to the next node in the list
};
here each node contains an integer data and a pointer which can store the address of a structure.The beginning of the list can be identified using a head pointer which stores the address of the starting node and the end of the list can be identified by a null pointer.
Example program
#include <stdio.h>
// Define a self-referential structure for a linked list node
struct Node {
int data; // Data of the node
struct Node *next; // Pointer to the next node in the list
};
int main() {
// Create three nodes
struct Node node1, node2, node3;
// Assign data to each node
node1.data = 10;
node2.data = 20;
node3.data = 30;
// Link the nodes to form a linked list
node1.next = &node2;
node2.next = &node3;
node3.next = NULL; // The last node's next is NULL, indicating the end of the list
// Traverse the linked list and print the data
struct Node *current = &node1; // Start from the first node
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next; // Move to the next node
}
printf("NULL\n");
return 0;
}
Comments
Post a Comment