Queue

 Analogy - Due to increased rush in the branch, branch manager decided to start token system to access banking services to the customers in the branch.

The person who used to come earlier to the bank used to get higher priority token than other people coming later than that person. Token having higher priority used to be processed first. 

Below is the figure showing tokens with their priority. Such type of data structure where the entry which gets generated first is processed first is called Queue data structure.


Definition - Queue is a linear data structure which holds collection of data where the data added first to the queue is processed first and the the  data added last to the queue is processed at last. That's why it is also called FIFO(First In First Out) data structure.



Variables and Operations On The Queue:

Front/Head Pointer - This is positional variable which locates the entry in the Queue who is ready for processing. 

Back/Tail/Rear Pointer - This is also the positional variable which indicates the position of last inserted entry in the Queue.

ENQUEUE()- This is the operation to add element in the Queue . Below figure shows the enqueue operation in the Queue.









DEQUEUE()- This is the operation to get and remove element pointed by Front/Head pointer from the Queue. Below figure shows the dequeue operation in the Queue.





PEEK() - This operation returns the value of Front/Head element of the queue without removing it from queue.





IsEmpty()- This operation is used to check if Queue is empty or not? If Queue is empty it will return true otherwise false.

IsFull() - This operation is used to check if Queue is full or not? If Queue is full it will return true otherwise false.

Please follow the below link to get an example of queue data structure.



Java has inbuilt class Queue to implement queue operation. Below example shows how to use Queue class provided by JAVA:

import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
       
        Queue<Integer> queue=new LinkedList<>();
        
        queue.add(1);//Adding element to the queue
        queue.add(3);
        queue.add(4);
         System.out.println("===All Opertion in itertion 1===");
          if(!queue.isEmpty())
          {
               System.out.println("Peek Element-"+queue.peek());//Front element of the queue
               System.out.println("removed Element-"+queue.remove());//Removing an element from the queue
               System.out.println("Peek Element-"+queue.peek());
               System.out.println("Is queue Empty-"+queue.isEmpty());//Check if queue is empty or not?
          }
          
          System.out.println("===All Opertion in itertion 2===");
          if(!queue.isEmpty())
          {
               System.out.println("Peek Element-"+queue.peek());
               System.out.println("removed Element-"+queue.remove());
               System.out.println("Peek Element-"+queue.peek());
               System.out.println("Is queue Empty-"+queue.isEmpty());
          }
          
          System.out.println("===All Opertion in itertion 3===");
          if(!queue.isEmpty())
          {
               System.out.println("Peek Element-"+queue.peek());
               System.out.println("removed Element-"+queue.remove());
               System.out.println("Is queue Empty-"+queue.isEmpty());
          }
    }
}


Previous Post Next Post