×

Self-referential structure in C

Self-referential structure in C

A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers pointing to the same type of structure as their member. The self-referential structure is widely used in dynamic data structures such as trees, linked lists, and so on. The next node of a node will be pointed in linked lists, which consists of the same struct type. It is a unique type of structure containing a member of its type. The member of its type is a pointer variable of the same structure in which it has been declared. In the context of blockchain, each block is linked to a previous node or a next node, similar to a linked list.

Syntax:

 struct structure_name
 {
 datatype datatype_name;
 structure_name * pointer_name;
 } 

E.g.:

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

Where ‘next’ is a pointer to a struct node variable, it should be remembered that the pointer to the structure is similar to the pointer to any other variable. Hence, a self-referential data structure is generally a structure definition that includes at least one member that is a pointer to the structure of its kind.

The self-referential structures are beneficial in many applications that involves linked data members, such as trees and lists. Unlike a static data structure such as an array where the size of the array limits the number of elements that can be inserted into the array, the self- referential structure can dynamically be contracted or expanded. Operations such as insertion or deletion of nodes in a self-referential structure involve simple alteration of the pointers present within them.

E.g.:

 typedef struct list
 {
 void *data;
 struct list *next;
 }linked_list;  

Here, in the above example, the list is self-referential structure as the *next is the type struct list.

 struct node
 {
 int data;
 char value;
 struct node * link;
 };
 int main()
 {
 struct node object;
 return 0;
 }  

In this particular example, ‘link’ is a pointer to f type ‘node’ structure. Hence, the structure ‘node’ is a self-referential structure with ‘link’ as a referencing pointer. The pointer should be appropriately initialized before accessing, as by default, it contains a garbage value.

Types of self-referential structures

  1. Self-referential structure with a single link: these structures are allowed to have only one self-pointer as their member.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 struct ref
 {
 int data;
 char val;
 struct ref* link;
 }
 int main()
 {
 struct ref object1;  //link1
 object1.link = NULL;
 object1.data = 10;
 object1.val = 20;
 struct ref object2; //
 object2.link = NULL;
 object2.data = 30;
 object2.val = 40;
 object1.link = &object2;
 printf (“%d \n”, object1.link -> data);
 printf (“%d \n”, object1.link -> val);
 return 0;
 } 

Output:

30 40
  • Self-referential structure with multiple links: these types of structures can have more than one self-pointers. Many complicated data structures can be easily constructed using these structures. Such structures can easily connect to more than one node at a time.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 struct ref
 {
 int data;
 struct ref* previous;
 struct ref* next;
 };
 int main()
 {
 struct node object1;
 object1.previous = NULL;
 object1.next = NULL;
 object1.data = 10;
 struct prev object2;
 object2.previous = NULL;
 object2.next = NULL;
 object2.data = 20;
 struct prev object3;
 object3.previous = NULL;
 object3.next = NULL;
 object3.data = 30;
 object1.next = &object2;//forward links
 object2.next = &object3;
 object2.next = &object1;//backward links
 object3.next = &object2;
 printf (“%d \t”, object1.data);
 printf (“%d \t”, object1.next -> data);
 printf (“%d \n”, object1.next -> next -> data);
 printf (“%d \t”, object2.prev -> data);
 printf (“%d \t”, object2.data);
 printf (“%d \n”, object2.next -> data);
 printf (“%d \t”, object3.prev -> prev -> data);
 printf (“%d \t”, object3.prev -> data);
 printf (“%d \n”, object3.data);
 return 0;
 } 

Output:

 10   20   30
 10   20   30
 10   20   30 

Here object1, object2, and object3 are three objects of self-referential structure ‘node’. They are connected by links in a way any other object can access each other’s data. Connections can be manipulated by a developer according to their requirements. The self- referential structure has its applications in data structures such as stacks, queues, binary trees, lists, graphs, etc.


Related Topics

#include in C

In the C programming language, #include is another way of inferring a standard, or a user defines file into the application or program. The preprocessor of the programming standard generally...

4 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Heap Sort in C

In this tutorial, we will learn about heap sorting in C language, but before going to Heap sort, we have to know the concept of Complete Binary Tree. What is Complete...

8 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Array Of Structures in C

The C programming language allows us to store multiple elements of the same type in arrays. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Flow chart of While loop in C

This is a flowchart that represents the process of executing the while loop in the C programming language.Generally, as we know there are three main components of while loop:1. The...

3 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.