A queue is a powerful Data Structure that controls the asynchronous flow of data through the buffering technique. Unlike Stack, a queue is used to insert data from one end and retrieve on the other.
This abstract data structure follows the First In First Out(FIFO) concept where the data stored first is accessed first.  We can implement queues using Structures, Arrays, Linked-lists or Pointers. Queue uses 2 data pointers – head and tail for Enqueue and Dequeue operations

Applications

Queue for boarding registration flight illustration.people with travel bags stand in line and wait until they are served by airport staff and allowed to board.

A ticketing system is a real-world example for the queue where the person first in the line is served first and the one at the end of the line is served last.  
A queue is a special Data Structure efficiently used for buffering. This concept works similar to the print queue where the jobs buffered are printed based on the order of requests received. We can also use queues to prioritize interrupts to address based on their priority.  Another simple example of a queue system is the first-come-first-served calls answered in a call-center. 

Queue Operations
The life cycle of a queue involves initializing, adding items to the queue (enqueue), retrieving items from the queue (dequeue) and deleting the queue from memory.
Learning Queue is incomplete without understanding the steps involved in Enqueue and Dequeue operations.

Enqueue – Enqueue operation manipulates only the tail pointer and does not hinder the head pointer.  We insert data in a queue with the following steps:

  1. Check if the queue is empty 
  2.  If the queue is not empty, stop the operation
  3. If the queue is empty, increment the tail pointer to point to the next available space
  4.  Insert the data in the newly created space
  5.  Now that becomes the current tail element of the queue
Algorithm for Enqueue:

   Enqueue(data)
   {
      if queue is empty     {
         tail = tail +1
         queue[tail] = data      
         }  
      else
          exit
    }

Dequeue – Dequeue operation does not manipulate the tail pointer. Here, data is removed from the Queue with the following steps:

  1.  Check if the queue is empty
  2.  If the queue is empty, stop the operation
  3.  If the queue is not empty, move to the head of the queue
  4.  Remove the data and increment the head pointer to the next position
  5.  Now that becomes the current head element of the queue 
Algorithm for Dequeue:

  

   Dequeue( )

    {

       if queue is empty

       exit

      else

       {

           head = head +1

           queue[head] = data      

       }  

    }

Types of Queues

  1.  A simple queue is a linear queue. We insert data until the queue becomes full.
  2.  Circular queue, commonly referred to as Ring buffer, is a linear queue where the head of the queue is  connected back to the tail. Data is enqueued in the head and dequeued from the tail. 

Learn more about Queues and other Data Structures and Algorithms Interview Questions from our books at Vibrant Publishers

previous arrow
next arrow
Slider
Shilpa Hegde

Author: Shilpa Hegde

Shilpa Hegde is a Software Engineer by profession and has a passion for writing on any niche. She has been writing poems, articles, blogs, Press Releases, reviews, to name a few. Some of her poems have been published on Sentinal Poetry movement, Stellar showcase Journal, and Blue Fog Journal. Her articles are published on Vivo group, adideo.com, broadwayworld.com, 4fastplumber, longboard-brand, and many other places.