Self referential structure in C

Introduction

अब तक हमने normal structures देखे, जहाँ structure के अंदर simple variables (int, float, array आदि) होते हैं।

लेकिन कुछ advanced situations में हमें ऐसे data structures की जरूरत होती है जहाँ एक element दूसरे element से जुड़ा होता है, जैसे:

  • Linked List
  • Trees
  • Graphs

इन सभी structures में हर element दूसरे element की information (address) रखता है।

ऐसी situations को handle करने के लिए C language में Self-referential Structure का उपयोग किया जाता है।

Self-referential structure में structure के अंदर ही उसी structure का pointer होता है।

यह concept बहुत important है क्योंकि:

  • Data Structures (Linked List) का base है
  • Exam में conceptual question आता है
  • Pointer + Structure दोनों का combination है

Definition

Self-referential Structure वह structure होता है जिसमें एक member उसी structure type का pointer होता है।

Basic Concept

Important Rule:

❌ Structure के अंदर उसी type का direct variable नहीं बना सकते
✔ लेकिन pointer बना सकते हैं

Example (Concept)

struct node
{
int data;
struct node *next;
};

Explanation:

  • data → value store करेगा
  • next → अगले node का address store करेगा

Why Pointer is Used?

गलत तरीका (Not Allowed):

struct node
{
int data;
struct node next; // ❌ error
};

क्यों error?

  • infinite memory size हो जाएगा

सही तरीका:

struct node *next;   // ✔ pointer

Example 1: Simple Self-referential Structure

#include <stdio.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node n1, n2;

n1.data = 10;
n2.data = 20;

n1.next = &n2;
n2.next = NULL;

printf("%d\n", n1.data);
printf("%d\n", n1.next->data);

return 0;
}

Output:

10
20

Explanation:

  • n1 → n2 को point कर रहा है
  • n1.next->data → n2 का data access किया

Example 2: Linked Structure Concept

#include <stdio.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node a, b, c;

a.data = 1;
b.data = 2;
c.data = 3;

a.next = &b;
b.next = &c;
c.next = NULL;

printf("%d -> %d -> %d",
a.data,
a.next->data,
a.next->next->data);
return 0;
}

Output:

1 -> 2 -> 3

Explanation:

  • nodes chain में जुड़े हैं
  • pointer के through traversal किया

Technical Understanding

Self-referential structures का use:

  • Linked List
  • Dynamic memory structures
  • Tree structures
  • Graph representation

Important Points

  • Structure के अंदर same type का pointer होता है
  • Direct structure variable allowed नहीं है
  • Pointer address store करता है
  • -> operator से access करते हैं

Difference: Normal vs Self-referential Structure

Normal StructureSelf-referential Structure
Simple data store करता हैLinked data store करता है
Pointer नहीं होताPointer होता है
Static structureDynamic structure

Exam Points

  • Self-referential structure = structure with pointer of same type
  • Linked list का base concept है
  • Direct variable allowed नहीं है
  • Pointer जरूरी है
  • -> operator important है

Leave a Comment

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

Scroll to Top