Self-Referential Structures and Linked Lists in C

A self-referential structure in C is a structure that contains a pointer to another structure of the same type. This concept is the foundation of many dynamic data structures like linked lists, trees, and graphs. In this tutorial, we will focus on how self-referential structures are used to build a linked list.

At SamagraCS Educational Technology, we make learning about self-referential structures and linked lists easy and intuitive, breaking down complex ideas into manageable steps.


What is a Self-Referential Structure?

A self-referential structure is a structure that contains a pointer to another structure of the same type. This allows the creation of data structures where each element can point to another element of the same type, forming chains of data like linked lists.

Real-Life Analogy:

Think of a linked list like a train where each carriage (node) is connected to the next one. Each carriage (structure) holds some data (passengers) and a pointer to the next carriage (the next structure). The last carriage in the train points to nothing, indicating the end of the list.


Defining a Self-Referential Structure (Linked List Node)

In a linked list, each element (or node) consists of:

  1. Data: The value or information stored in the node.
  2. Pointer: A pointer that points to the next node in the list.

Syntax:

Here:

  • data: This is where the node stores its value.
  • next: This is a pointer that points to the next node in the linked list. It’s a self-referential pointer because it points to another structure of type Node.

What is a Linked List?

A linked list is a dynamic data structure that consists of a sequence of nodes. Each node stores data and a pointer to the next node. Linked lists are commonly used because they provide efficient insertion and deletion operations compared to arrays.

There are three main types of linked lists:

  1. Singly Linked List: Each node points to the next node.
  2. Doubly Linked List: Each node points to both the next and the previous node.
  3. Circular Linked List: The last node points back to the first node.

In this tutorial, we will focus on the singly linked list.


Basic Operations on a Linked List

  1. Inserting a node: Adding a new node to the list.
  2. Deleting a node: Removing a node from the list.
  3. Traversing the list: Going through each node and printing its data.

Implementing a Linked List in C

Let’s walk through the implementation of a basic singly linked list in C.

Example: Creating and Traversing a Linked List

Output:


Explanation of the Code:

  1. Structure Definition:
  • We define the structure Node, which contains an integer data and a pointer next that points to the next node in the linked list.
  1. createNode() Function:
  • This function dynamically allocates memory for a new node, assigns the data to the node, and sets the next pointer to NULL (indicating the end of the list).
  1. Linking Nodes:
  • The nodes are linked together using the next pointer. For example, head->next = second; links the first node (head) to the second node.
  1. Traversing the List:
  • The printList() function starts from the head node and traverses the list, printing the data of each node until it reaches the end (NULL).

Inserting a Node at the Beginning of the List

Let’s modify the program to insert a new node at the beginning of the list.

Code Example:

Output:


Explanation of Insertion Code:

  1. Inserting at the Beginning:
  • The insertAtBeginning() function creates a new node and makes it point to the current head of the list. The new node then becomes the new head of the list.
  1. Updating the Head:
  • We update the head to point to the new node after each insertion. This ensures that the new node is added at the beginning of the list.

Real-Life Example: Student Records Using Linked List

Let’s create a linked list to store a collection of student records, where each student has a name and roll number.

Code Example:

Output:


A self-referential structure is an essential concept in C programming

that enables the creation of dynamic data structures like linked lists. By using self-referential pointers, you can link nodes together to form chains of data and efficiently manage collections of records.

At SamagraCS Educational Technology, we believe in providing easy-to-understand tutorials with practical applications. Try implementing more linked list operations such as deletion and searching to get a deeper understanding of how self-referential structures work.

For any questions or assistance, feel free to reach out to Pawan & Pooja, or the team at SamagraCS Educational Technology. Keep learning and happy coding!

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?