Know your Queue Data Structures in 60 seconds

by Vibrant Publishers
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 it 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:

A ticketing system is a real-world example off a 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 similarly 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:

 

The 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 not 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. A circular queue, commonly referred to as a 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

       

       

       

      To ace your interview, you can explore related topics. Learn about other data structures through the following blogs on our website: 

      1. Basics of Stack Data Stucture 
      2. Trees
      3. Basics of hash data structures
      These provide insights into the various types of data structures and will give you better understanding.

       

      Get one step closer to your dream job!

       

      Pave the route to your dream job by preparing for your interview with questions from our Job Interview Questions Book Series. These provide a comprehensive list of questions for your interview regardless of your area of expertise. These books include:
      1. HR Interview Questions You’ll Most Likely Be Asked (Third Edition) 
      2. Innovative Interview Questions You’ll Most Likely Be Asked
      3. Leadership Interview Questions You’ll Most Likely Be Asked
      You can find them on our website!