Javatpoint Logo
Javatpoint Logo

Insertion in doubly linked list after Specified node

In order to insert a node after the specified node in the list, we need to skip the required number of nodes in order to reach the mentioned node and then make the pointer adjustments as required.

Use the following steps for this purpose.

  • Allocate the memory for the new node. Use the following statements for this.
  • Traverse the list by using the pointer temp to skip the required number of nodes in order to reach the specified node.
  • The temp would point to the specified node at the end of the for loop. The new node needs to be inserted after this node therefore we need to make a fer pointer adjustments here. Make the next pointer of ptr point to the next node of temp.

make the prev of the new node ptr point to temp.

make the next pointer of temp point to the new node ptr.

make the previous pointer of the next node of temp point to the new node.

Algorithm

  • Step 1: IF PTR = NULL
  •    Write OVERFLOW
       Go to Step 15
     [END OF IF]

  • Step 2: SET NEW_NODE = PTR
  • Step 3: SET PTR = PTR -> NEXT
  • Step 4: SET NEW_NODE -> DATA = VAL
  • Step 5: SET TEMP = START
  • Step 6: SET I = 0
  • Step 7: REPEAT 8 to 10 until I
  • Step 8: SET TEMP = TEMP -> NEXT
  • STEP 9: IF TEMP = NULL
  • STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
  •    GOTO STEP 15
       [END OF IF]
     [END OF LOOP]

  • Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT
  • Step 12: SET NEW_NODE -> PREV = TEMP
  • Step 13 : SET TEMP -> NEXT = NEW_NODE
  • Step 14: SET TEMP -> NEXT -> PREV = NEW_NODE
  • Step 15: EXIT

Insertion in doubly linked list after Specified node

C Function

Output

Enter the item which you want to insert?
12

Node Inserted

Press 0 to insert more ?
0

Enter the item which you want to insert?
90

Node Inserted

Press 0 to insert more ?
2

Next TopicDoubly Linked List





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA