Doubly Linked list(DLL)
DLL is a solution to the drawbacks of SLL
Drawbacks of SLL
In SLL, each node provides information about where the next node is. It has no
knowledge about where the previous node is.
If we are at the ith node in the list currently, then to access the (i 1)th node or (i
2)th node, we have to traverse the list right from the first node.
In addition, it is not possible to delete the ith node given only a pointer to the ith
node. It is also not possible to insert a node before the ith node given only
a pointer to the ith node
For handling such difficulties, we can use DLLs where each node contains two links,
one to its predecessor and other to its successor
Fig. Doubly linked list of four nodes
Each node of a DLL has three fields in general but must have at least two link fields
Fig. Node structure of doubly linked list
The class of a doubly linked list node:
class DLL_Node
{
Public:
int Data;
DLL_Node *Prev, *Next;
DLL_Node()
{
Prev = Next = NULL;
}
};
A DLL may either be linear or circular. DLLs are also called two-way lists.
The DList class has only one data member, the Head pointer,
Head pointer points to the first node of the list, which is used to access the list.
The member functions including the constructor and the destructor are used to process
the list.
Here Head is private and all other member functions are public. To ensure that nodes
are not accessible to outside objects Head is declared as private. Other members are
declared as public to be accessible to outside objects.
Data structure of node
Each node has data and two link fields such as next and prev
The data field holds data element(s) and the link field(s) stores the address of its
successor and predecessor.
Every node is a group of two (or more) data elements which are of different data
types, they are logically grouped using the data type, object.
The link fields of a node is a pointer that references to a node of the same type as
itself. Hence, a self-referential object is required
The declaration of the data structure of a node is given as follows:
class DLL_Node
{
public:
int data;
DLL_Node *prev, *next;
DLL_Node()
{
prev = next = NULL;
}
};
DLL may or may not contain a header node.
Insertion of a node in a doubly Linked List
Inserting a node in DLL has different logic for the following different cases.
Inserting between two nodes
Inserting at the beginning
Inserting at the end
Inserting between two nodes
To insert a node, say Current, we have
to modify four links as each node points to its predecessor as well as successor. Let us
assume that the node Current is to be inserted in between the two nodes say node1 and
node2.
Fig. Inserting a node current
The statements to insert a node in between node1 and node2 are as follows:
node1->Next = Current;
node2->Prev = Current;
Current->Next = node2;
Current->Prev = node1;
This is shown in Fig.
After the insertion of node Current, the resultant modified links should be shown as in
Fig.
Fig. Link modification for insertion of a node in a DLL
Inserted a node at the beginning:
Head is pointing to first node. Current is the node to be inserted at the
beginning.
To insert a node at the first position
Fig. Inserting a node before first node
Then, the statements could be
Current->Next = Head;
Head->Prev = Current;
Head = Current;
Deletion of a node from a doubly Linked List
Deleting a node in DLL has different logic for the following different cases.
Deleting between any two nodes
Deleting at the beginning
Deleting at the end
Deleting between any two nodes:
First we have to traverse the list to reach to the node to be deleted, we call that node as
curr
Second we implement the following steps to insert the node:
(curr->Prev)->Next = curr->Next;
(curr->Next)->Prev = curr->Prev;
delete curr;
curr refers to the current node,
prev is a link to the predecessor node and
next is a link to the successor node.
Deleting at the beginning
Head is pointer pointing to the first node
Head=curr->next;
Head->prev=NULL;
delete curr;
Deleting at the end
(curr->prev)->next=NULL;
delete curr;