Deletion in a Doubly Linked List in C

Just like insertion, we have three scenarios while deleting a node in a doubly linked list:

  • Deletion from the beginning of the list.
  • Deletion from the end of the list.
  • Deletion at specific position of the list.

a. Deletion at the Beginning of the Doubly Linked List

To delete a node at the beginning of the doubly linked list, follow the below approach:

Approach:

  • Check if the List is Empty: If true, there’s nothing to delete.
  • Check if the List Contains Only One Node:
    • Set head to NULL.
    • Free the memory of the node.
  • If the List Contains More Than One Node:
    • Update head to head->next.
    • Set the prev of the new head to NULL.
    • Free the memory of the old head.

Time Complexity: O(1), as deletion is done at front so we are not traversing through the list.

b. Deletion at the End of the Doubly Linked List

To delete a node at the end of the doubly linked list, follow the below approach:

Approach:

  • Check if the List is Empty: If true, there’s nothing to delete.
  • If the List is Not Empty:
    • Traverse to the last node using a loop.
    • Check if the last node is the only node (head->next == NULL):
      • Update head to NULL.
    • If more than one node:
      • Set the next of the second last node (last->prev) to NULL.
    • Free the memory of the last node.

Time Complexity: O(n), as deletion is done at the end, so we need to traverse through the list. If a tail pointer is maintained, this operation can be optimized to O(1) by directly accessing the last node.

c. Deletion at a Specific Position in Doubly Linked List

For deleting a node at a specific position in a doubly linked list, follow the below approach:

Approach:

  • Check Position Validity:
    • If position is 0 (or 1 depending on indexing), use deletion at the beginning.
    • If position is the size of the list, use deletion at the end.
    • Otherwise, proceed with the middle deletion.
  • Traverse to the Specified Position:
    • Use a loop to reach the node (delNode) at the desired position.
    • If delNode is the first node, adjust head.
    • If not, adjust delNode->prev->next and delNode->next->prev if delNode->next is not NULL.
  • Free the Memory:
    • Release the node to be deleted.

Time Complexity: O(n), as deletion is done in between, as we may need to traverse up to n elements to find the correct position.

Doubly Linked List in C

A doubly linked list is a type of linked list in which each node contains 3 parts, a data part and two addresses, one points to the previous node and one for the next node. It differs from the singly linked list as it has an extra pointer called previous that points to the previous node, allowing the traversal in both forward and backward directions.

In this article, we will learn about the doubly linked list implementation in C. We will also look at the working of the doubly linked list and the basic operations that can be performed using the doubly linked list in C.

Similar Reads

Doubly Linked List Representation in C

A doubly linked list is represented in C as the pointer to the head (i.e. first node) in the list. Each node in a doubly linked list contains three components:...

Implementation of Doubly Linked List in C

To implement a doubly linked list in C first, we need to define a node that has three parts: data, a pointer to the next node, and a pointer to the previous node, and then create a new node....

Basic Operations on C Doubly Linked List

We can perform the following basic operations in a doubly-linked list:...

1. Insertion in Doubly Linked List in C

Inserting a new node in a doubly linked list can be done in a similar way like inserting a new node in a singly linked list but we have to maintain the link of previous node also. We have three scenarios while inserting a node in a doubly linked list:...

2. Deletion in a Doubly Linked List in C

Just like insertion, we have three scenarios while deleting a node in a doubly linked list:...

3. Traversal in a Doubly Linked List in C

Traversal in a doubly linked list means visiting each node of the doubly linked list to perform some kind of operation like displaying the data stored in that node. Unlike singly linked list we can traverse in doubly linked list in both the directions....

C Program to Implement Doubly Linked List

The below program demonstrates all the major operations of a doubly linked list: insertion (at the beginning, at the end, and at a specific position), deletion (at the beginning, at the end, and at a specific position), and traversal (forward and reverse)....

Advantages of Doubly Linked List

Unlike singly linked list, we can traverse in both the direction forward and reverse using doubly linked list which makes operations like reversing and searching from end easier and more efiicient.It is fast and easier to delete a node in doubly linked list, if the node to be deleted is given then we can search the previous node quickly and update it’s links.Inserting a new node is also easier in doubly linked list, as we can simply insert a new node before a given node by updating the links.In a doubly linked list, we need not to keep track of the previous node during traversal that is very useful in data structures like the binary tree where we need to keep track of the parent node....

Disadvantages of Doubly Linked List

Although doubly linked lists provide more flexibility and efficiency in certain operations compared to singly linked lists, they also consume more memory as they need to store an extra pointer for each node.The insertion and deletion code in a doubly linked list is more complex as we need to maintain the previous links too.For insertion and deletion at a specific position in a doubly linked list we need to traverse from a head node to the specified node that can be time-consuming in case of larger lists.For each node we have to maintain two pointers in a doubly linked list that leads to the overhead of maintaining and updating these pointers during insertions and deletions....

Frequently Asked Questions on Doubly Linked List

What is a Doubly Linked List (DLL)?...