Spread the post

Insert node at beginning of linked list

How to insert node at beginning of linked list?. let discussing about all process. what is structure of node:

Data field: how many number of data field are need depends upon your requirement.

Pointer field : how many number of pointer are need. any number are pointer are used here. but normally at least one pointer are compulsory that are pointed to next linked list node.

Here simple linked list node structure are define.

Suppose following data are inserting in linked list. [496, 437, 496, 501, 479, 450, 465, 451, 569, 586 ]. Initially linked list are empty. and linked list root pointer are null. Step by step inserting all linked list node.

Algorithm:

Step 1: Create memory block on heap area.

Step 2: Assign data and pointer value.

Step 3: Assign root pointer .

Continuous follow this three step to insert linked list node at start position.

Example: Insert node 496.

Inserted node 496 at beginning Linked list example to insert element at start position. root_ptr 496

live preview

Insert another element 437.

Inserted node 437 at beginning Linked list example to insert element at start position. root_ptr496437

live preview

Insert another element 496.

Inserted node 496 at beginning Linked list example to insert element at start position. root_ptr496437496

live preview

Insert another element 501

Inserted node 501 at beginning Linked list example to insert element at start position. root_ptr496437496501

live preview

Insert another element 479.

Inserted node 479 at beginning Linked list example to insert element at start position. root_ptr496437496501479

live preview

Insert another element 450

Inserted node 450 at beginning Linked list example to insert element at start position. root_ptr496437496501479450

live preview

Insert another element 465

Inserted node 465 at beginning Linked list example to insert element at start position. root_ptr496437496501479450465

live preview

Insert another element 451

Inserted node 451 at beginning Linked list example to insert element at start position. root_ptr496437496501479450465451

live preview

Insert another element 569

Inserted node 569 at beginning Linked list example to insert element at start position. root_ptr496437496501479450465451569

live preview

Insert another element 586

Inserted node 586 at beginning Linked list example to insert element at start position. root_ptr496437496501479450465451569586

live preview

Execution process:

Stack Areamainroot(pointer)insert_nodenew_node(pointer)temp(pointer)value (int) =465 Heap Areastruct Nodedata (int)= 496next (pointer)= NULLstruct Nodedata (int)= 437next (pointer) struct Nodedata (int)= 496next (pointer) struct Nodedata (int)= 501next (pointer) struct Nodedata (int)= 479next (pointer) struct Nodedata (int)= 450next (pointer) struct Nodedata (int)= 465next (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.

View code execution

C program to inserting linked list node at beginning.

Output
 Case 1: Empty Linked list 
   Output Empty linked List

 case 2: When linked list are not empty 
   Output Linked list data is : 586  569  451  465  450  479  501  496  437  496  
 case 3: Free Of linked list Node 
    
   Free linked list Node element 

Spread the post

Recommended Posts: