Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Insert node at middle of linked list

How to insert data at middle (mid-position) of singly linked list?. Here given following process to insert node.

Process to insert node at middle of linked list

Step 1: Create memory block on heap area.

Step 2: When memory are allocate using malloc function then Assign data value to newly created node.

Step 3: If no element in this linked list then assign this newly created memory block address to root pointer

Step 4: In this process to finding the mid-element of linked list that process time are complexity O(n).

Step 5: When Exist two or more linked list nodes element.

Step 6: If only one element in linked list node.

Visualization :Suppose given linked list is given below. View and editing given this linked list.

Try it yourself

Inserting this newly created node 777 on mid-position after linked list is.

After inserted middle node

Suppose following data [ 44, 35, 34, 144, 23, 36, 66, 62] are inserted step by step.

Initial linked list are empty then insert first element 44.

root_ptr44

View nodes and pointer.This is base condition of insert first element of linked list.

Global Variableroot(pointer) Stack Areamaininsert_mid_nodenew_node(pointer)value (int) =44 Heap Areastruct Nodedata (int)= 44next (pointer)= NULL

Insert second element on middle position. note that only single element are in linked list. then in this case insert node after first node of linked list. insert data 35.

root_ptr3544 Global Variableroot(pointer) Stack Areamaininsert_mid_nodehelp(pointer)new_node(pointer)temp(pointer)value (int) =35 Heap Areastruct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 35next (pointer)= NULL

insert another data:34

root_ptr353444

look at this change next pointer positions.

Global Variableroot(pointer) Stack Areamaininsert_mid_nodehelp(pointer)new_node(pointer)temp(pointer)value (int) =34 Heap Areastruct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 35next (pointer)= NULLstruct Nodedata (int)= 34next (pointer)

insert data : 144

root_ptr351443444 Global Variableroot(pointer) Stack Areamaininsert_mid_nodenew_node(pointer)value (int) =144 Heap Areastruct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 35next (pointer)= NULLstruct Nodedata (int)= 34next (pointer) struct Nodedata (int)= 144next (pointer)

Insert another node data:23.

root_ptr35144233444 Global Variableroot(pointer) Stack Areamaininsert_mid_nodenew_node(pointer)value (int) =23 Heap Areastruct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 35next (pointer)= NULLstruct Nodedata (int)= 34next (pointer) struct Nodedata (int)= 144next (pointer) struct Nodedata (int)= 23next (pointer)

similarly insert remaining data [36, 66, 62]. visualize insert data in animated view

View linked list

After insert other element linked list.

root_ptr35144366266233444

Code imagination : Insert middle element of linked list that process are given below. step by step code execution process are available here. see and imagize c code execution to insert middle element of linked list.

insert node at middle execution process

Time complexity of this program O(n)

Click To Visualize Code

Given below C program to insert element at middle of linked list.

Output
Linked list data is : 44  34  23  66  62  36  144  35  

 Free linked list Node element 
  

Spread the post

Recommended Posts: