Deletion at the Beginning of doubly linked list

Deletion at the Beginning in Doubly Linked List

To delete a node at the beginning in doubly linked list, we can use the following steps:

  • Check If the list is empty, there is nothing to delete. Return.
  • Check if the node to be deleted is the head node, update the head pointer to point to the next node.
  • If the node to be deleted is not the head node, update the previous pointer of the next node to point to the previous node of the node to be deleted.
  • If the node to be deleted is not the tail node, update the next pointer of the previous node to point to the next node of the node to be deleted.
  • Free the memory allocated to the node to be deleted.

Below is the implementation of the above approach:

C++
// Structure to represent a node in a doubly linked list
struct Node {
    int data; // Data stored in the node
    Node* next; // Pointer to the next node in the list
    Node* prev; // Pointer to the previous node in the list
};

// Function to delete the node at the beginning of a doubly
// linked list
void deleteAtBeginning(Node** head) {
    // Check if the list is empty
    if (*head == nullptr) {
        return;
    }

    // Check if the node to be deleted is the head node
    if (*head == (*head)->next) {
        // If the node to be deleted is the head node,
        // update the head pointer to point to the next node
        *head = (*head)->next;

        // If the list has only one node, set the head
        // pointer to NULL
        if (*head == nullptr) {
            return;
        }

        // Update the previous pointer of the new head node
        // to point to NULL
        (*head)->prev = nullptr;
        
        // Delete the old head node
        delete *head;
        return;
    }

    // If the node to be deleted is not the head node,
    // update the previous pointer of the next node to point
    // to the previous node of the node to be deleted
    Node* temp = *head;
    *head = (*head)->next;
    (*head)->prev = nullptr;

    // Delete the node to be deleted
    delete temp;
}

Doubly Linked List Tutorial

A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This is because each node in the list contains a pointer to the previous node and a pointer to the next node. This allows for quick and easy insertion and deletion of nodes from the list, as well as efficient traversal of the list in both directions.

Doubly Linked List in Data Structure

Similar Reads

What is a Doubly Linked List?

A doubly linked list is a data structure that consists of a set of nodes, each of which contains a value and two pointers, one pointing to the previous node in the list and one pointing to the next node in the list. This allows for efficient traversal of the list in both directions, making it suitable for applications where frequent insertions and deletions are required....

Representation of Doubly Linked List in Data Structure:

In a data structure, a doubly linked list is represented using nodes that have three fields:...

Node Definition:

Here is how a node in a Doubly Linked List is typically represented:...

Operations on Doubly Linked List:

Traversal in Doubly Linked ListSearching in Doubly Linked ListFinding Length of Doubly Linked ListInsertion in Doubly Linked List:Insertion at the beginning of Doubly Linked ListInsertion at the end of the Doubly Linked ListInsertion at a specific position in Doubly Linked ListDeletion in Doubly Linked List:Deletion of a node at the beginning of Doubly Linked ListDeletion of a node at the end of Doubly Linked ListDeletion of a node at a specific position in Doubly Linked List...

Traversal in Doubly Linked List:

To Traverse the doubly list, we can use the following steps:...

Finding Length of Doubly Linked List:

To find the length of doubly list, we can use the following steps:...

Insertion at the Beginning in Doubly Linked List:

Insertion at the Beginning in Doubly Linked List...

Insertion at the End of Doubly Linked List

Insertion at the End in Doubly Linked List...

Insertion at a Specific Position of Doubly Linked List

...

Deletion at the Beginning of doubly linked list

Deletion at the Beginning in Doubly Linked List...

Deletion at the End on doubly linked list:

Deletion at a End in Doubly Linked List...

Deletion at a Specific Position in doubly linked list

Deletion at a Specific Position in Doubly Linked List...

Advantages of Doubly Linked List

Efficient traversal in both directions: Doubly linked lists allow for efficient traversal of the list in both directions, making it suitable for applications where frequent insertions and deletions are required.Easy insertion and deletion of nodes: The presence of pointers to both the previous and next nodes makes it easy to insert or delete nodes from the list, without having to traverse the entire list.Can be used to implement a stack or queue: Doubly linked lists can be used to implement both stacks and queues, which are common data structures used in programming....

Disadvantages of Doubly Linked List

More complex than singly linked lists: Doubly linked lists are more complex than singly linked lists, as they require additional pointers for each node.More memory overhead: Doubly linked lists require more memory overhead than singly linked lists, as each node stores two pointers instead of one....

Applications of doubly linked lists:

Implementation of undo and redo functionality in text editors.Cache implementation where quick insertion and deletion of elements are required.Browser history management to navigate back and forth between visited pages.Music player applications to manage playlists and navigate through songs efficiently.Implementing data structures like Deque (double-ended queue) for efficient insertion and deletion at both ends....