×

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

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

4 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.