Self Referential Structure

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Self-Referential Structures

 The structures in the linked list need not be contiguous in memory.


o They are ordered by logical links that are stored as part of the data in
the structure itself.
o The link is a pointer to another structure of the same type.

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

 Self-referential structures are the structures which contain a pointer /


member field pointing to the same structure type.
 Self-referential structures are those structures which contains reference
to itself.
 A self-referential structure is used to create data structures like linked
lists, stacks, etc.

Nodes of the list:


struct node a, b, c;
a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;

Chaining these together


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

What are the values of?:


• a.next->data
• a.next->next->data
Example Program
#include<stdio.h>
struct node
{
int data;
struct node *next; Output:
}; Value of a:1
int main() Value of b:2
{
Value of c:3
struct node a,b,c;
a.data=1;
b.data=2;
c.data=3;
a.next=&b;
b.next=&c;
c.next=NULL;
printf("\n Value of a:%d",a.data);
printf("\n Value of b:%d",a.next->data);
printf("\n Value of c:%d",b.next->data);
return 0;
}

You might also like