Insertion in Doubly Linked List in C++

In a doubly linked list, there are three ways in which the insertion operation can be performed:

  • Insertion at the beginning of DLL
  • Insertion at the end of DLL
  • Insertion at Specific Position in DLL

a. Insertion at the Beginning in Doubly Linked List

Insertion at Beginning- Doubly Linked List

First, create a new node to be inserted and then follow the below approach to insert a new node at the beginning of a doubly linked list:

Approach:

  • First, create a newNode
  • If the list is empty:
    • set newNode->next to NULL.
    • set newNode->prev to NULL.
    • point the head pointer to newNode.
  • If the list is not empty:
    • set newNode->next to head.
    • set newNode->prev to NULL.
    • set head->prev to newNode.
    • point the head pointer to newNode.

b. Insertion at the End in Doubly Linked List

Insertion at End- Doubly Linked List

First, create a new node to be inserted and then follow the below approach to insert a new node at the end of a doubly linked list:

Approach:

  • First, create a newNode.
  • If the list is empty:
    • set newNode->next to NULL.
    • set newNode->prev to NULL.
    • point the head pointer to newNode.
  • If the list is not empty:
    • find the last node by iterating the list (i.e. where last->next == NULL).
    • set last->next to newNode.
    • set newNode->prev to last.
    • set newNode->next to NULL.

c. Insertion at Specific Position in Doubly Linked List

Insertion at Specific Position- Doubly Linked List

First, create a new node to be inserted and then follow the below approach to insert a new node at a given position in a doubly linked list:

Approach:

  • First, create a newNode.
  • Check whether the given position is valid or not by finding the size of the list.
  • If the given position is 0 (or 1 depending on indexing):
    • we can use the insertion at the beginning approach only.
  • If the given position is the last i.e. it is equal to the size of the list:
    • we can use the insertion at the end approach here.
  • If the given position is valid then do the following:
    • start traversing the list to find the node that comes immediately before the given position (call it prevNode).
    • set newNode->next to prevNode->next.
    • set newNode->prev to prevNode.
    • if prevNode->next is not NULL, set prevNode->next->prev to newNode.
    • set prevNode->next to newNode.

Doubly Linked List in C++

A Doubly Linked List (DLL) is a two-way list in which each node has two pointers, the next and previous that have reference to both the next node and previous node respectively. Unlike a singly linked list where each node only points to the next node, a doubly linked list has an extra previous pointer that allows traversal in both the forward and backward directions.

In this article, we will learn about the doubly linked list representation, implementation of doubly linked list in C++, basic operations that can be performed on doubly linked list, its advantages and disadvantages.

Similar Reads

Doubly Linked List Representation in C++

In a doubly linked list, two pointers are maintained to keep track of the list i.e. head (that points to the first node) and tail (that points to the last node)....

Implementation of Doubly Linked List in C++

To implement all the basic operations that can be performed on a doubly linked list, we need to first define a node. Let’s see how we can create a new node in a doubly linked list....

Basic Operations on C++ Doubly Linked List

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

1. Insertion in Doubly Linked List in C++

In a doubly linked list, there are three ways in which the insertion operation can be performed:...

2. Deletion in Doubly Linked List in C++

Like insertion, there are three ways in which the deletion operation can be performed:...

3. Traversal in a Doubly Linked List in C++

Traversing a doubly linked list means iterating through the list by visiting each node and performing the desired operations. This can be done by maintaining a temp variable that starts from the head of the doubly linked list and follows the next pointer for traversing until the end of the list. In a doubly linked list, we can traverse in both directions:...

C++ Program to Implement Doubly Linked List

The below program demonstrates the implementation of all the basic operations on the doubly linked list....

Advantages of Doubly Linked List in C++

The following are the advantages of a doubly linked list in C++:...

Disadvantages of Doubly Linked List in C++

It requires more memory than arrays, per node due to the additional storage used by the pointers.It is more complex to implement and maintain as compared to the singly-linked list.We have to traverse from the head node to the specific node for insertion and deletion at specific positions....

Frequently Asked Questions on Doubly Linked in C++

What is the Primary Advantage of a Doubly Linked List over a Singly Linked List?...