Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Explain insertion of elements in linked list using C language
A linked list is a linear data structure where elements (called nodes) are stored in sequence, with each node containing data and a pointer to the next node. Unlike arrays, linked list elements are stored in non-contiguous memory locations and connected through pointers.
Syntax
struct node {
int data;
struct node *next;
};
void insert_front(struct node **head, int value);
void insert_end(struct node **head, int value);
void insert_after(struct node *head, int value, int after);
void insert_before(struct node **head, int value, int before);
Linked List Representation
Each node in a linked list contains two components −
- Data: The actual value stored in the node
- Next: A pointer to the next node in the sequence
Insertion Operations
There are four main types of insertion operations in a linked list −
- Insert at Beginning: Add a new node at the start
- Insert at End: Add a new node at the end
- Insert After: Add a new node after a specific node
- Insert Before: Add a new node before a specific node
Complete Implementation
Here's a complete C program demonstrating all insertion operations −
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void print_list(struct node *head) {
printf("List: ");
while (head != NULL) {
printf("%d", head->data);
if (head->next != NULL) printf(" -> ");
head = head->next;
}
printf(" -> NULL
");
}
void insert_front(struct node **head, int value) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Memory allocation failed
");
return;
}
new_node->data = value;
new_node->next = *head;
*head = new_node;
}
void insert_end(struct node **head, int value) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Memory allocation failed
");
return;
}
new_node->data = value;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
return;
}
struct node *temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = new_node;
}
void insert_after(struct node *head, int value, int after) {
struct node *temp = head;
while (temp != NULL && temp->data != after) {
temp = temp->next;
}
if (temp == NULL) {
printf("Node with value %d not found
", after);
return;
}
struct node *new_node = (struct node *)malloc(sizeof(struct node));
if (new_node == NULL) {
printf("Memory allocation failed
");
return;
}
new_node->data = value;
new_node->next = temp->next;
temp->next = new_node;
}
int main() {
struct node *head = NULL;
printf("Creating initial list with values 1, 2, 3:
");
insert_end(&head, 1);
insert_end(&head, 2);
insert_end(&head, 3);
print_list(head);
printf("\nInserting 0 at the beginning:
");
insert_front(&head, 0);
print_list(head);
printf("Inserting 4 at the end:
");
insert_end(&head, 4);
print_list(head);
printf("Inserting 5 after node with value 2:
");
insert_after(head, 5, 2);
print_list(head);
return 0;
}
Creating initial list with values 1, 2, 3: List: 1 -> 2 -> 3 -> NULL Inserting 0 at the beginning: List: 0 -> 1 -> 2 -> 3 -> NULL Inserting 4 at the end: List: 0 -> 1 -> 2 -> 3 -> 4 -> NULL Inserting 5 after node with value 2: List: 0 -> 1 -> 2 -> 5 -> 3 -> 4 -> NULL
Key Points
-
Memory Management: Always check if
malloc()returns NULL and free allocated memory when needed - Time Complexity: Insert at front is O(1), while insert at end and positional inserts are O(n)
- Pointer Manipulation: Be careful with pointer assignments to maintain list integrity
Conclusion
Insertion operations in linked lists provide flexibility for dynamic data management. Understanding pointer manipulation and proper memory allocation is crucial for implementing these operations correctly and efficiently.
Advertisements
