Insertion
- In a double linked list, the insertion operation can be performed
in three ways as follows...
1.
At Beginning of the
list
2.
Inserting At End of
the list
3.
Inserting At
Specific location in the list
Inserting
at Beginning of the list - We
can use the following steps to insert a new node at beginning of the double
linked list...
·Step1: Create a new_node with given value and new_node → previous as NULL.
·Step 2: Check whether list is Empty (head== NULL)
·Step3: If it is Empty then, assign NULL to new_node → next and new_node to head.
·Step 4: If it is not Empty then, assign head to new_node → next and new_node to head.
Inserting
at End of the list - We
can use the following steps to insert a new node at end of the double linked
list...
·Step 1: Create a new_node with given value and new_node → next as NULL.
·Step 2: Check whether list is Empty (head== NULL)
·Step 3: If it is Empty, then assign NULL to new_node → previous and new_node to head.
·Step 4: If it is not Empty, then, define a node pointer ptr and initialize
with head.
·Step 5: Keep moving the ptr to its next node until it reaches to the last
node in the list (until ptr
→ next is equal to NULL).
·Step 6: Assign new_node to ptr → next and ptr to new_node → previous.
Inserting
At Specific location in the list (After a Node) -
We can use the following steps to insert a new node after a node
in the double linked list...
·Step 1: Create a new_node with given value.
·Step 2: Check whether list is Empty (head== NULL)
·Step3: If it is Empty then, assign NULL to new_node→ previous & new_node
→ next and new_node to head.
·Step 4: If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
·Step 5: Keep moving the temp1 to its next node until it reaches to the
node after which we want to insert the new_node (until temp1 → data is equal to location, here location is the node value after which
we want to insert the new_node).
·Step 6: Every time check whether temp1 is reached to
the last node. If it is reached to the last node then display 'Given node is not found in the list!!!
Insertion not possible!!!'and terminate the function. Otherwise
move the temp1 to
next node.
·Step 7: Assign temp1
→ next to temp2, new_node to temp1 → next, temp1 to new_node → previous, temp2 to new_node → next and new_node to temp2 → previous.