×

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

Expressions in C

Expressions in C: In the C programming language, an expression defines a formula in which the operands are linked to each other by using operators to compute the value. The operand...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Diffie-Hellman Algorithm in C

Background: A method of public-key encryption known as elliptic curve cryptography (ECC) is based on the algebraic structure of elliptic curves over finite fields. To ensure equal security, ECC encryption requires fewer...

4 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Multi-dimensional Arrays in C

Multi-dimensional Arrays in C The C programming allows the concept of multi-dimensional arrays. The data within the multidimensional arrays are stored in the tabular form, that is, in a row significant...

3 minutes read.

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

5 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 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.

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.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.