What is Queue?

As the name suggests, the queue is the type of data structure that follows the FIFO (First In – First Out) mechanism. In simple words, it is a linear data structure that resembles somewhere like a stack but has the difference of entry and exit in the elements i.e unlike stacks. It is open at both ends. One end is used to insert data (enqueue) and the other end is used to remove data (dequeue).

Queues are just like lines at a ticket counter or a cash-counter where the first entering person is the first exiting person as well.

There is a similar operation of the queue just like the stacks. Some of the operations include:

peek() ? Gets the first element of queue without

removing it.

isfull() ? Checks if it is full.

isempty() ? Checks if it is empty.

These 3 above operations are efficient enough to serve the major two operations of queues and they are:

Enqueue():  Adds or stores item in the queue.

Dequeue():  Removes or prevents access of items in the queue.

Let us now look at some pseudo-codes for understanding operations in queue.

  1. Peek()
  • isFull()
  • isEmpty()

The above operational codes are just the minor steps to approach a problem involving implementations of queues. Let us look at a program to understand the flow control of queues better.


Queue in C++
Queue in C++


The above code depicts the flow for adding and removing elements in a queue. To understand how the enqueue and dequeue operations are performed, look at the following algorithm below.


1.Initialize a variable of any choice

  1. Read the variable
  2. If(top == 1) then
  3. call insert function 
  4. Else
  5. call delete function
  6. End

Algorithm for inserting in Array-Queue:

  1. If rear = NULL
  2. rear=front=0
  3. QUEUE[0]=ITEM }
  4. Else If rear= N-1 then
  5. Print “Queue Overflow!”
  6. Else
  7. QUEUE[rear+1]=ITEM
  8. rear=rear+1
  9. END.

Algorithm for deleting in Array-Queue:

1.If front==NULL then

2. Print “Queue is Empty” 

3. Else

4. ITEM=QUEUE[front]

5. If front=rear Then

6. front=rear=NULL

7. Else

8. front = front + 1

9. END

Note: The above algorithm works for mainly two operations namely enqueue and dequeue. Since these two operations are also associated with other sub-operations like peek(), isFull(), and isEmpty() we can also easily them from the above code.

Analysis of features and implementations

  1. Queues are widely used in sharing single resource serving requests like print, or task scheduling, etc.
  • Practically, queues are used in the same way the call-centers hold the calls until the representatives are free.
  • Queues can be used to handle real-time interrupts in the same order they arrive like first in first out.

Complexity Analysis

  1. Enqueue : O(1)
  2. Dequeue : O(1)
  3. Size : O(1)

Note: Queues are used where processing is not required. Processing may not need any priority since the priority queue follows a different approach. Queues follow the same approach as Breadth-First-Search (BFS).

Points to Remember:

  1. The point of entry and exit are different in a queue.
  2. Tw0 stacks can be used to make a queue.
  3. Random access is not allowed in the queue.
  4. We cannot simply add or remove elements from the middle of any queue.
  5. Inserting operation using an array is costly in the dequeue. This is because all the elements at a specific position need to be shifted by one. It is the same concept of similar people sitting on a bench and one person from one end pushes down the other to accommodate himself.

Pin It on Pinterest

Share This