0
0
Data Structures Theoryknowledge~6 mins

Insertion and deletion operations in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a list of items, like a shopping list, and you want to add new things or remove some you no longer need. Insertion and deletion operations help us manage collections of data by adding or removing elements efficiently.
Explanation
Insertion
Insertion means adding a new element into a data structure, such as a list, array, or tree. The position where the element is added depends on the type of data structure and the rules it follows. For example, in a list, you can insert at the beginning, middle, or end.
Insertion adds new elements to a data structure at a specific position.
Deletion
Deletion means removing an existing element from a data structure. Like insertion, the element to remove is chosen based on the data structure's rules. After deletion, the structure may need to adjust to fill the gap left by the removed element.
Deletion removes elements and may require rearranging the data structure.
Insertion in Arrays vs Linked Lists
In arrays, insertion can be costly if it requires shifting many elements to make space. In linked lists, insertion is easier because you only change pointers to add a new node without moving other elements. This difference affects performance.
Insertion is simpler in linked lists than arrays because linked lists avoid shifting elements.
Deletion in Arrays vs Linked Lists
Deleting an element from an array often requires shifting elements to fill the empty space. In linked lists, deletion involves changing pointers to bypass the removed node, which is usually faster and does not require moving other elements.
Deletion is more efficient in linked lists since it avoids shifting elements.
Real World Analogy

Think of a row of lockers. Adding a new locker in the middle means shifting all lockers after it to the right to make space, like inserting in an array. But if the lockers are connected by chains, you can just add or remove a locker by changing the chain links, like in a linked list.

Insertion → Adding a new locker in the row
Deletion → Removing a locker from the row
Insertion in Arrays vs Linked Lists → Shifting lockers to make space vs changing chain links
Deletion in Arrays vs Linked Lists → Shifting lockers to fill space vs unlinking a locker from the chain
Diagram
Diagram
Array before insertion:
[ A | B | C | D ]

Insert 'X' at position 2:
[ A | X | B | C | D ]
  ↑    ↑
Shift elements right

Linked list before insertion:
A -> B -> C -> D

Insert 'X' after A:
A -> X -> B -> C -> D
  ↑    ↑
Change pointers only
This diagram shows how insertion works differently in arrays and linked lists, highlighting element shifting versus pointer changes.
Key Facts
InsertionAdding a new element to a data structure at a specific position.
DeletionRemoving an existing element from a data structure.
Array insertion costInsertion in arrays may require shifting elements, making it slower.
Linked list insertion costInsertion in linked lists involves changing pointers, which is faster.
Array deletion costDeletion in arrays often requires shifting elements to fill gaps.
Linked list deletion costDeletion in linked lists involves pointer updates without shifting.
Code Example
Data Structures Theory
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def insert_after(self, prev_node, data):
        if not prev_node:
            return
        new_node = Node(data)
        new_node.next = prev_node.next
        prev_node.next = new_node

    def delete_node(self, key):
        temp = self.head
        if temp and temp.data == key:
            self.head = temp.next
            temp = None
            return
        prev = None
        while temp and temp.data != key:
            prev = temp
            temp = temp.next
        if temp is None:
            return
        prev.next = temp.next
        temp = None

    def print_list(self):
        temp = self.head
        while temp:
            print(temp.data, end=' -> ' if temp.next else '\n')
            temp = temp.next

# Create list and perform insertion and deletion
ll = LinkedList()
ll.head = Node('A')
second = Node('B')
third = Node('C')
ll.head.next = second
second.next = third

print('Original list:')
ll.print_list()

ll.insert_after(second, 'X')
print('After inserting X after B:')
ll.print_list()

ll.delete_node('B')
print('After deleting B:')
ll.print_list()
OutputSuccess
Common Confusions
Insertion always means adding at the end.
Insertion always means adding at the end. Insertion can happen at any position, not just the end, depending on the data structure.
Deletion removes the element but leaves empty space.
Deletion removes the element but leaves empty space. After deletion, data structures usually rearrange elements or pointers to avoid gaps.
Arrays and linked lists handle insertion and deletion the same way.
Arrays and linked lists handle insertion and deletion the same way. Arrays require shifting elements, while linked lists only update pointers, making their operations different.
Summary
Insertion adds new elements to data structures at specific positions, which may require shifting or pointer changes.
Deletion removes elements and usually involves rearranging the structure to avoid gaps.
Arrays require shifting elements for insertion and deletion, while linked lists use pointer updates, making them more efficient for these operations.