Linked List Operations in C

 

In data structures, a linked list is a very important concept. It is used to store data in a dynamic way. Unlike arrays, a linked list does not require continuous memory. Instead, it stores elements in different memory locations and connects them using pointers.

Each element in a linked list is called a node. A node has two parts:

  1. Data part – stores the value

  2. Pointer part – stores the address of the next node

Because of this structure, linked lists are flexible and memory efficient.

Structure of a Node

In C, we use struct to create a node.

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

Here data stores the value and next stores the address of the next node.

Basic Operations of Linked List

There are several operations we can perform on a linked list. The most common ones are insertion, deletion, and traversal.

1. Traversal

Traversal means visiting each node of the linked list and printing or processing its data.

Example code:

void display(struct Node* head) {
struct Node* temp = head;

while(temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
}

This function starts from the first node and moves to the next node until it reaches NULL.

2. Insertion

Insertion means adding a new node to the linked list. It can be done in different positions.

Insertion at Beginning

In this operation, the new node becomes the first node.

Steps:

  1. Create a new node

  2. Point new node to the current head

  3. Update head to the new node

Example:

void insertBeginning(struct Node** head, int value) {
struct Node* newNode = malloc(sizeof(struct Node));

newNode->data = value;
newNode->next = *head;
*head = newNode;
}

This operation is very fast because we only change one pointer.

Insertion at End

In this operation, the new node is added at the end of the list.

Steps:

  1. Create a new node

  2. Traverse to the last node

  3. Make last node point to the new node

Example:

void insertEnd(struct Node** head, int value) {
struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

struct Node* temp = *head;

while(temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
}

3. Deletion

Deletion means removing a node from the linked list.

Deletion at Beginning

This is the easiest deletion operation.

Steps:

  1. Store the head node

  2. Move head to next node

  3. Free the first node

Example:

void deleteBeginning(struct Node** head) {
struct Node* temp = *head;
*head = (*head)->next;
free(temp);
}

Deletion at End

Steps:

  1. Traverse to the second last node

  2. Remove the last node

  3. Update pointer

Example:

void deleteEnd(struct Node* head) {
struct Node* temp = head;

while(temp->next->next != NULL) {
temp = temp->next;
}

free(temp->next);
temp->next = NULL;
}

Advantages of Linked List

Linked lists have several advantages compared to arrays:

  • Dynamic size (can grow or shrink easily)

  • Efficient insertion and deletion

  • No need for continuous memory allocation

Because of these advantages, linked lists are widely used in many applications like memory management, implementing stacks and queues, and graph algorithms.

Conclusion

Linked list is a very useful data structure in C programming. It helps in storing and managing data dynamically. Understanding linked list operations such as insertion, deletion, and traversal is very important for learning data structures and algorithms. Once we understand linked lists clearly, it becomes easier to study more advanced topics like trees and graphs.

Comments