Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views9 pages

Insertion Operations in Singly Linkedlist

The document discusses insertion operations in a singly linked list, detailing methods for inserting nodes at the beginning, in between, and at the end of the list. It emphasizes the efficiency of these operations and the importance of understanding linked lists for managing dynamic data structures. Additionally, it highlights the advantages of implementing linked lists in C, such as direct memory access and educational value.

Uploaded by

test.user190625
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Insertion Operations in Singly Linkedlist

The document discusses insertion operations in a singly linked list, detailing methods for inserting nodes at the beginning, in between, and at the end of the list. It emphasizes the efficiency of these operations and the importance of understanding linked lists for managing dynamic data structures. Additionally, it highlights the advantages of implementing linked lists in C, such as direct memory access and educational value.

Uploaded by

test.user190625
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

INSERTION OPERATIONS IN SINGLY LINKEDLIST

NAME - AKASH SAHA

UNIVERSITY ROLL NO. - 10900224184

SUBJECT NAME: DATA STRUCTURE & ALGORITHMS

STREAM : IT 2ND YEAR

SECTION: C

CLASS ROLL : 150


Insertion in a Singly Linked List
A linked list is a fundamental dynamic data structure composed of nodes, where each node holds data and a
reference to the next node in the sequence. Unlike arrays, linked lists allow for efficient memory usage as their size
can grow or shrink during runtime. One of the most important operations performed on linked lists is insertion, which
involves adding a new node at a specified position. This can be done at the beginning, in between existing nodes,
or at the end of the list. Understanding how to perform these insertion operations is essential for managing dynamic
data effectively and serves as a foundation for more complex data structures
Insertion at the Beginning
Description:
When inserting a node at the beginning, the new node becomes the first node of the list, and the head pointer must be updated to point to this new node.
This operation is quick and efficient.

Steps:

Allocate memory for the new node.


Assign the new data to this node.
Make the next pointer of the new node refer to the current head.
Update the head pointer to point to the new node.

void insertAtBeginning(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
Insertion in Between (After a Given Node)
To insert a node in the middle of the list, we need to specify the node after which the new node will be inserted. This often requires traversing the list to
find the correct position.

Steps:

Ensure the given previous node is not NULL.


Allocate memory for the new node.
Assign the new data.
Make the next pointer of the new node point to the next of the previous node.
Update the next pointer of the previous node to the new node.

void insertAfter(struct Node* prev_node, int new_data) {


if (prev_node == NULL) {
printf("The given previous node cannot be NULL\n");
return;
}
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
Insertion at the End
Inserting at the end means adding the new node after the current last node in the list. If the list is empty, the new node becomes the head. Otherwise,
traverse the list until the last node is reached.

Steps:

Allocate memory for the new node.


Assign the data and set the next pointer of the new node to NULL.
If the list is empty (head is NULL), make the new node the head.
Else traverse to the end of the list.
Link the last node's next pointer to the new node.

void insertAtEnd(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;

if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

while (last->next != NULL) {


last = last->next;
}

last->next = new_node;
}
Summary of Linked List Insertion in
C
Insertion is a key operation that allows dynamic growth of linked lists.
Three main insertion types:
At the beginning: Efficient constant-time operation by updating the
head pointer.
In between: Requires locating the correct node, involves traversal.
At the end: Traverses list to append the new node.
Mastering insertion is essential for understanding more complex data
structures like stacks, queues, and trees.
Linked lists efficiently handle memory without the need for contiguous
memory allocation, unlike arrays.
C provides direct memory management with pointers, making linked
list implementation more explicit and educational.
Advantages of Implementing Linked List
Insertion in C
Direct Memory Access: C allows manipulation of memory addresses via pointers,
giving precise control over dynamic memory allocation.
Efficiency: Insertion at the beginning is O(1), and C's low-level features ensure
minimal overhead.
Educational Value: Understanding pointers in C helps build a strong foundation in
data structure fundamentals.
Flexibility: C enables implementing complex data structures from scratch without
built-in abstractions.
Performance: Low-level operations in C often lead to faster runtime compared to
higher-level languages
References
"Data Structures Using C" by Reema Thareja 4 A comprehensive guide on implementing data structures in C.
"Let Us C" by Yashavant Kanetkar 4 Covers C programming basics and data structure implementations.
"The C Programming Language" by Brian W. Kernighan and Dennis M. Ritchie 4 Classic resource on C programming fundamentals.
THANK YOU
Akash Saha

You might also like