In the previous tutorial, we learned about the Singly Linked List. In this tutorial, we will learn about the doubly linked list.
If you don’t know about the singly linked list, read it here.
In the singly linked list, we keep the address of the next node. Here in the doubly linked list for every node, we will keep the address of the next node as well as the previous node. Thus we can go forward and backward.
Node representation in Doubly Linked List
A node is represented as
// Creating a node
class Node {
public:
int value;
Node* next;
Node* previous;
};
Insertion in Doubly Linked List
Suppose we want to add a new node named newNode after a node named curNode.
newNode->next=curNode->next;
newNode->prev=curNode;
curNode->next=newNode;
Deletion in Doubly Linked List
Suppose we want to delete a node name curNode. we can do this following way,
curNode->prev->next=curNode->next;
curNode->next->prev=curNode->prev;