Spread the post

How to implement linked list using queue data structure?

The One of Most interesting question is why to use queue data structure of linked list. The important factors is they are providing Constant time O(1) to insert linked list element.

Algorithm: Simple step to insert linked list element.

Step 1: Create memory block on heap area.

/* create new memory block using malloc function.*/
struct Node*new_node=(struct Node*)malloc(sizeof(struct Node));

Step 2: Assign data and pointer value.

/*Assign data and next pointer value to newly created node.*/
new_node->data=value;
new_node->next=NULL;

Step 3: if linked list is empty Assign front and rear pointers to address of newly created node .

/*Assign newly created block address to rear and front pointers. */
*front=new_node;
*rear=new_node; /*Otherwise do step 4.*/

Step 4 : if linked list are not empty Assign rear pointer to address of newly created node .

/*Assign newly created block address to rear pointer. */
*rear=new_node;

Example : Suppose following data are insert on linked list is[ 44, 35, 34, 75, 635, 623, 23, 36, 66, 62 ].

root_ptr 6266362362363575343544

Linked list basic Animation: View and Edit Linked list Click Here.

Code Executrion Process: Below in show. And more execution process Click Here

Stack Areamainfront(pointer)rear(pointer)en_queuefront(pointer)new_node(pointer)rear(pointer)value (int) =62 Heap Areastruct Nodedata (int)= 44next (pointer) struct Nodedata (int)= 35next (pointer) struct Nodedata (int)= 34next (pointer) struct Nodedata (int)= 75next (pointer) struct Nodedata (int)= 636next (pointer) struct Nodedata (int)= 612next (pointer) struct Nodedata (int)= 23next (pointer) struct Nodedata (int)= 36next (pointer) struct Nodedata (int)= 66next (pointer) struct Nodedata (int)= 62next (pointer)= NULL

Code implementation: Below in C program.

Output
 Case 1: Empty Linked list 
 	 Output Empty linked List

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

 Free linked list Node element   

Spread the post