Doubly Linked List
Doubly Linked List
Doubly linked list is a type of linked list in which each node apart from storing its data has two
links. The first link points to the previous node in the list and the second link points to the next
node in the list. The first node of the list has its previous link pointing to NULL similarly the
last node of the list has its next node pointing to NULL.
The two links help us to traverse the list in both backward and forward direction. But storing an
extra link requires some extra space.Now we define our class Doubly Linked List. It has the
following methods:
Insertion
A node can be added in four ways
1) At the front of the DLL
2) After a given node.
3) At the end of the DLL
4) Before a given node.
struct node
{
int data; // Data
node *prev; // A reference to the previous node
node *next; // A reference to the next node
};
class Doubly_Linked_List
{
node *front; // points to first node of list
node *end; // points to last node of list
public:
Doubly_Linked_List()
{
front = NULL;
end = NULL;
}
void add_front(int );
void add_after(node* , int );
void add_before(node* , int );
void add_end(int );
void delete_node(node*);
void forward_traverse();
void backward_traverse();
};
1) Add a node at the front:
The new node is always added before the head of the given Linked List. And newly
added node becomes the new head of DLL.
// List is empty
if(front == NULL)
end = temp;
else
front->prev = temp;
front = temp;
}
// if list is empty
if(end == NULL)
front = temp;
else
end->next = temp;
end = temp;
}
4) Add a node before a given node:
Let the pointer to this given node be next_node and the data of the new node to be
added as new_data.
Check if the next_node is NULL or not. If it’s NULL, return from the function because any
new node can not be added before a NULL
Allocate memory for the new node, let it be called new_node
Set new_node->data = new_data
Set the previous pointer of this new_node as the previous node of the next_node, new_node-
>prev = next_node->prev
Set the previous pointer of the next_node as the new_node, next_node->prev = new_node
Set the next pointer of this new_node as the next_node, new_node->next = next_node;
If the previous node of the new_node is not NULL, then set the next pointer of this previous
node as new_node, new_node->prev->next = new_node
Else, if the prev of new_node is NULL, it will be the new head node. So, make (*head_ref)
= new_node.
Remove a Node:
Removal of a node is quite easy in Doubly linked list but requires special handling if the node to
be deleted is first or last element of the list. Unlike singly linked list where we require the
previous node, here only the node to be deleted is needed. We simply make the next of the
previous node point to next of current node (node to be deleted) and prev of next node point to
prev of current node. Look code for more details.
Forward Traversal:
Start with the front node and visit all the nodes untill the node becomes NULL.
Backward Traversal:
Start with the end node and visit all the nodes until the node becomes NULL.