Insertion in Doubly Linked Lists
Alright so for this post we are going to tackle a very basic question- What do we do when we want to go anywhere in the linked list?
The thing with a normal single linked list is that it is unidirectional which in essence is easy to understand but less efficient to work with, it brings with it a set of advantages and disadvantages. To tackle some basic disadvantages we include another pointer in the node family, Enter 'Prev' pointer. This pointer along with 'Next' pointer works seamlessly to define the position of a node with respect to not only what is after that node but also what is in front of that node.
Some of it's advantages are -
- A doubly linked list can be traversed in both directions
- The deletion in DLL is more efficient if pointer to the node to be deleted is given
- We can quickly insert a new node.
void insertatfront(Node * head, int new_data)
{
Node* new_node= new Node(new_data);
new_node->next= head;
new_node->prev=NULL;
if(head!=NULL)
head->prev= new_node;
head=new_node;
}
Here we have defined a new node with pointers prev and next, prev points to NULL value and next points to the old head value, then we update the head to the new node value as Head must always stay in front.
Function: Add a node after a given node
void addafter(Node* previous_node, int new_data)
{
if(previous_node=NULL)
return;
Node* new_node= new Node(new_data);
new_node->prev=previous_node;
new_node->next=previous_node->next;
if(new_node->next!=NULL)
new_node->next->prev=new_node;
}
Here we have defined a new node with its previous pointer pointing to the previous node value, it's next pointer pointing to the previous node's next pointer, The if condition is needed to update the previous pointer of new node.
Function: Add node at the end
void addend(Node* head, int new_data)
{
Node* new_node= new Node(new_data);
new_node->next=NULL;
if(head==NULL)
new_node->prev= NULL;
head= new_node;
return;
while(last->next!=NULL)
last=last->next;
last->next=new_node;
new_node->prev=last;
return;
}
In this specific function we have a new node, The if condition checks if head is empty or not and the while loop traverses until the last node, once we have traversed to the end of the list, we add our new_node and define the next pointer as NULL and prev pointer as last node value.
Function: Add a node before a given node
void addbefore(Node* head, Node* next_node, int new_data)
{
Node* new_node= new Node(new_data);
new_node->prev=next_node->prev;
next_node->prev=new_node;
new_node->next=next_node;
if(new_node->prev!=NULL)
new_node->prev->next=new_node;
else
nead=new_node;
}
Here we have a new_node which is added before next_node, the pointers of new_node i.e Next points to next_node, the Prev pointer points to prev of the next_node, If the new_node's previous pointer is not pointing to null, then we update the next previous pointer of the new node as the new_node value, hence making sure the new_node remains at the specified position, An else condition is added in case the prev pointer points to NULL which means it is the first node in the whole list, hence Head is updated as new_node.
Oh wow, that was a lot of explaining, I must say i've got quite a grasp on the subject of linked lists now!
Next post is going to be something different! See you soon guys!