Insertion at the Beginning in Doubly Linked List

Insertion at the Beginning in Doubly Linked List

To insert a new node at the beginning of the doubly list, we can use the following steps:

  • Allocate memory for a new node and assign the provided value to its data field.
  • Set the previous pointer of the new node to nullptr.
  • If the list is empty:
    • Set the next pointer of the new node to nullptr.
    • Update the head pointer to point to the new node.
  • If the list is not empty:
    • Set the next pointer of the new node to the current head.
    • Update the previous pointer of the current head to point to the new node.
    • Update the head pointer to point to the new node.

Below are the implementation of the above approach:

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

// Function to insert a node at the beginning of the list
void insertBeginning(Node*& head, int value) {
    // Create a new node
    Node* newNode = new Node;

    // Assign the value to the new node
    newNode->data = value;

    // Since it's the first node, the previous pointer is nullptr
    newNode->prev = nullptr;

    // If the list is empty, make the new node the head
    if (head == nullptr) {
        // Since it's the only node, the next pointer is nullptr
        newNode->next = nullptr;

        // Update the head pointer to point to the new node
        head = newNode;
    } else {
        // Point the new node's next pointer to the current head
        newNode->next = head;

        // Update the previous pointer of the current head to point to the new node
        head->prev = newNode;

        // Update the head pointer to point to the new node
        head = newNode;
    }
}

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....