Spread the post

Inserted Linked list data in Ascending order (sorted order)

Inserting linked list element in ascending (sorted order). We Discussing about all test cases. Following data [44, 35, 34, 75, 23, 36, 66, 62] are inserting into linked list.

Case 1: No element in linked list then insert first node of linked list. Suppose insert node 44.

Insert first node Insert first element of linked list root_ptr 44

Case 2: If insert data value less then and equal to root pointed value then insert node at starting position. Suppose insert node 35.

Insert node at beginning Insert node at beginning position root_ptr4435

Onother example insert newly data 34.

Insert node at beginning Insert node at beginning position root_ptr443534

View this execution process.

Try it yourself

Case 3: If insert node are big then insert those node at end of linked list. insert element 75.

Insert node at End of linked list Insert node at End of linked list root_ptr75443534

Insert element 23. That are follow case 2.

Insert node at beginning Insert node at beginning position root_ptr7544353423

Case 4: Special case when insert newly created node at between of linked list nodes.

Insert element 36.

Insert node at between node Insert node at between node root_ptr754436353424

Insert element 66.

Insert node at between node Insert node at between node root_ptr75664436353424

insert node 62.

Insert node at between Insert node at between nodes root_ptr7566624436353424

Algorithm:

Given code

Given Below execution process

Process to Insert Node Execution process to insert linked list node. Global Variableroot(pointer) Stack Areamaininsert_ascending_ordernew_node(pointer)temp(pointer)value (int) =23 Heap Areastruct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 35next (pointer) struct Nodedata (int)= 34next (pointer)= NULLstruct Nodedata (int)= 75next (pointer) struct Nodedata (int)= 23next (pointer)= ?

In this execution process not imagination all steps. below link click and view how to insert linked list node, how to show all linked list node and free all linked list element.

Click To Visualize Code

Time complexity of this program O(n)

C program to insert linked list node in ascending order.

Output
  Case 1: Empty Linked list 
   Output Empty linked List

 case 2: When linked list are not empty 
   Output Linked list data is : 23  34  35  36  44  62  66  75  

 Free linked list Node element 
  

Spread the post