Queue in C++
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.
- Peek()
int peek_or_top() {
return queue[front_element];
}
- isFull()
bool isfull()
{
if(rear == MaximumSize - 1)
return true;
else
return false;
}
- isEmpty()
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
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.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#define SIZE 5
using namespace std;
int q[SIZE],front=0,rear=0;
int main()
{
int ch;
void enqueue();
void dequeue();
void display();
while(1)
{
cout<<"\n 1. add element";
cout<<"\n 2. remove element";
cout<<"\n 3.display";
cout<<"\n 4.exit";
cout<<"\n enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
}
}
}
void enqueue()
{
int no;
if (rear==SIZE && front==0)
cout<<"queue is full";
else
{
cout<<"enter the num:";
cin>>no;
q[rear]=no;
}
rear++;
}
void dequeue()
{
int no,i;
if (front==rear)
cout<<"queue is empty";
else
{
no=q[front];
front++;
cout<<"\n"<<no<<" removed from the queue\n";
}
}
void display()
{
int i,temp=front;
if (front==rear)
cout<<"the queue is empty";
else
{
cout<<"\n element in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
}
}
}
Output:


Explanation:
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.
START
1.Initialize a variable of any choice
- Read the variable
- If(top == 1) then
- call insert function
- Else
- call delete function
- End
Algorithm for inserting in Array-Queue:
- If rear = NULL
- rear=front=0
- QUEUE[0]=ITEM }
- Else If rear= N-1 then
- Print "Queue Overflow!"
- Else
- QUEUE[rear+1]=ITEM
- rear=rear+1
- 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
- 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
- Enqueue : O(1)
- Dequeue : O(1)
- 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:
- The point of entry and exit are different in a queue.
- Tw0 stacks can be used to make a queue.
- Random access is not allowed in the queue.
- We cannot simply add or remove elements from the middle of any queue.
- 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.