JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Linked List implementation of Queue

Linked List implementation of Queue

Due to the drawbacks discussed in the previous section of this tutorial, the array implementation can not be used for the large scale applications where the queues are implemented. One of the alternative of array implementation is linked list implementation of queue.
The storage requirement of linked representation of a queue with n elements is o(n) while the time requirement for operations is o(1).
In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each element of the queue points to its immediate next element in the memory.
In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer. The front pointer contains the address of the starting element of the queue while the rear pointer contains the address of the last element of the queue.
Insertion and deletions are performed at rear and front end respectively. If front and rear both are NULL, it indicates that the queue is empty.
The linked representation of queue is shown in the following figure.

Linked List implementation of Queue 

Operation on Linked Queue

There are two basic operations which can be implemented on the linked queues. The operations are Insertion and Deletion.

Insert operation

The insert operation append the queue by adding an element to the end of the queue. The new element will be the last element of the queue.
Firstly, allocate the memory for the new node ptr by using the following statement.
  1. Ptr = (struct node *) malloc (sizeof(struct node));  
There can be the two scenario of inserting this new node ptr into the linked queue.
In the first scenario, we insert element into an empty queue. In this case, the condition front = NULL becomes true. Now, the new element will be added as the only element of the queue and the next pointer of front and rear pointer both, will point to NULL.
  1. ptr -> data = item;  
  2.         if(front == NULL)  
  3.         {  
  4.             front = ptr;  
  5.             rear = ptr;    
  6.             front -> next = NULL;  
  7.             rear -> next = NULL;  
  8.         }  
In the second case, the queue contains more than one element. The condition front = NULL becomes false. In this scenario, we need to update the end pointer rear so that the next pointer of rear will point to the new node ptr. Since, this is a linked queue, hence we also need to make the rear pointer point to the newly added node ptr. We also need to make the next pointer of rear point to NULL.
  1. rear -> next = ptr;  
  2.             rear = ptr;  
  3.             rear->next = NULL;  
In this way, the element is inserted into the queue. The algorithm and the C implementation is given as follows.

Algorithm

  • Step 1: Allocate the space for the new node PTR
  • Step 2: SET PTR -> DATA = VAL
  • Step 3: IF FRONT = NULL
    SET FRONT = REAR = PTR
    SET FRONT -> NEXT = REAR -> NEXT = NULL
    ELSE
    SET REAR -> NEXT = PTR
    SET REAR = PTR
    SET REAR -> NEXT = NULL
    [END OF IF]
  • Step 4: END

C Function

  1. void insert(struct node *ptr, int item; )  
  2. {  
  3.       
  4.       
  5.     ptr = (struct node *) malloc (sizeof(struct node));  
  6.     if(ptr == NULL)  
  7.     {  
  8.         printf("\nOVERFLOW\n");  
  9.         return;  
  10.     }  
  11.     else  
  12.     {   
  13.         ptr -> data = item;  
  14.         if(front == NULL)  
  15.         {  
  16.             front = ptr;  
  17.             rear = ptr;   
  18.             front -> next = NULL;  
  19.             rear -> next = NULL;  
  20.         }  
  21.         else   
  22.         {  
  23.             rear -> next = ptr;  
  24.             rear = ptr;  
  25.             rear->next = NULL;  
  26.         }  
  27.     }  
  28. }     

Deletion

Deletion operation removes the element that is first inserted among all the queue elements. Firstly, we need to check either the list is empty or not. The condition front == NULL becomes true if the list is empty, in this case , we simply write underflow on the console and make exit.
Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy the node pointed by the front pointer into the pointer ptr. Now, shift the front pointer, point to its next node and free the node pointed by the node ptr. This is done by using the following statements.
  1. ptr = front;  
  2.         front = front -> next;  
  3.         free(ptr);  
The algorithm and C function is given as follows.

Algorithm

  • Step 1: IF FRONT = NULL
    Write " Underflow "
    Go to Step 5
    [END OF IF]
  • Step 2: SET PTR = FRONT
  • Step 3: SET FRONT = FRONT -> NEXT
  • Step 4: FREE PTR
  • Step 5: END

C Function

  1. void delete (struct node *ptr)  
  2. {  
  3.     if(front == NULL)  
  4.     {  
  5.         printf("\nUNDERFLOW\n");  
  6.         return;  
  7.     }  
  8.     else   
  9.     {  
  10.         ptr = front;  
  11.         front = front -> next;  
  12.         free(ptr);  
  13.     }  
  14. }   

Menu-Driven Program implementing all the operations on Linked Queue

  1. #include<stdio.h>   
  2. #include<stdlib.h>  
  3. struct node   
  4. {  
  5.     int data;   
  6.     struct node *next;  
  7. };  
  8. struct node *front;  
  9. struct node *rear;   
  10. void insert();  
  11. void delete();  
  12. void display();  
  13. void main ()  
  14. {  
  15.     int choice;   
  16.     while(choice != 4)   
  17.     {     
  18.         printf("\n*************************Main Menu*****************************\n");  
  19.         printf("\n=================================================================\n");  
  20.         printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");  
  21.         printf("\nEnter your choice ?");  
  22.         scanf("%d",& choice);  
  23.         switch(choice)  
  24.         {  
  25.             case 1:  
  26.             insert();  
  27.             break;  
  28.             case 2:  
  29.             delete();  
  30.             break;  
  31.             case 3:  
  32.             display();  
  33.             break;  
  34.             case 4:  
  35.             exit(0);  
  36.             break;  
  37.             default:   
  38.             printf("\nEnter valid choice??\n");  
  39.         }  
  40.     }  
  41. }  
  42. void insert()  
  43. {  
  44.     struct node *ptr;  
  45.     int item;   
  46.       
  47.     ptr = (struct node *) malloc (sizeof(struct node));  
  48.     if(ptr == NULL)  
  49.     {  
  50.         printf("\nOVERFLOW\n");  
  51.         return;  
  52.     }  
  53.     else  
  54.     {   
  55.         printf("\nEnter value?\n");  
  56.         scanf("%d",&item);  
  57.         ptr -> data = item;  
  58.         if(front == NULL)  
  59.         {  
  60.             front = ptr;  
  61.             rear = ptr;   
  62.             front -> next = NULL;  
  63.             rear -> next = NULL;  
  64.         }  
  65.         else   
  66.         {  
  67.             rear -> next = ptr;  
  68.             rear = ptr;  
  69.             rear->next = NULL;  
  70.         }  
  71.     }  
  72. }     
  73. void delete ()  
  74. {  
  75.     struct node *ptr;  
  76.     if(front == NULL)  
  77.     {  
  78.         printf("\nUNDERFLOW\n");  
  79.         return;  
  80.     }  
  81.     else   
  82.     {  
  83.         ptr = front;  
  84.         front = front -> next;  
  85.         free(ptr);  
  86.     }  
  87. }  
  88. void display()  
  89. {  
  90.     struct node *ptr;  
  91.     ptr = front;      
  92.     if(front == NULL)  
  93.     {  
  94.         printf("\nEmpty queue\n");  
  95.     }  
  96.     else  
  97.     {   printf("\nprinting values .....\n");  
  98.         while(ptr != NULL)   
  99.         {  
  100.             printf("\n%d\n",ptr -> data);  
  101.             ptr = ptr -> next;  
  102.         }  
  103.     }  
  104. }  
Output:
***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
123

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?1

Enter value?
90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

123

90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?2

***********Main Menu**********

==============================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?3

printing values .....

90

***********Main Menu**********

==============================

1.insert an element
2.Delete an element
3.Display the queue
4.Exit

Enter your choice ?4

.