Queue Data Structure Introduction
Queue Data Structure Introduction
A Queue is another type of Data Structure, generally done via arrays. Generally, Queues
are FIFO i.e. First in First out. A good example would be people standing at a billing counter in a
queue, the person who comes first, will be served first.
Queue Operations
Enqueue: Adding a new item to the Queue Data Structure, in other words, enqueuing new
item to Stack DS.
Dequeue: Removing an item from the Queue, i.e. dequeuing an item out.
IsEmpty: This returns True If the Queue is empty else returns False
IsFull: This returns True if the Queue is full else returns false.
Some other notations are Front and Rear, that return the front end read items of the queue.
Representation of Queue
Queue as a data structure can be represented in two ways.
1. Enqueue()
When we require to add an element to the Queue we perform Enqueue() operation.
Push() operation is synonymous of insertion/addition in a data structure.
2. Dequeue()
When we require to delete/remove an element to the Queue we perform Dequeue()
operation.
Dequeue() operation is synonymous of deletion/removal in a data structure.
Applications and uses for Queues
Heavily used in almost all applications of the operating system, to schedule processes,
moving them in or out of process scheduler.
FCFS, SJF etc
Asynchronously i.e. when data resource may be the same but not received at the same
rate.
Anything that has to do with process and schedule, in the system or code.
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
Queue* queue = createQueue(1000);
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
return 0;
}
Output:
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue
Front item is 20
Rear item is 40
Queue in C Programming
Queue is FIFO type i.e. First in First out. In queues inserting an item is called as Enqueue and
removing an item is called as Dequeue.
Terminologies
Enqueue means adding one item to the queue and happens at the rear end of queue
Dequeue means removal of one item from the queue and happens at the front end of the
queue
If the max size of queue is reached then the queue is said to be in Overflow condition
If the queue has no items then queue is said to be in Underflow condition
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek() {
return intArray[front];
}
bool isEmpty() {
return itemCount == 0;
}
bool isFull() {
return itemCount == MAX;
}
int size() {
return itemCount;
}
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int removeData() {
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
int main() {
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
if(isFull()) {
printf("Queue is full!\n");
}
insert(16);
insert(17);
insert(18);
printf("----------------------\n");
printf("index : 5 4 3 2 1 0\n");
printf("----------------------\n");
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
}
Output:
Queue is full!
Element removed: 3
Element at front: 5
----------------------
index : 5 4 3 2 1 0
----------------------
Queue: 5 9 1 12 15 16
You can think of queues as a queue of people at airport ticket counter. The first person to
enter the queue is served by the air hostess at ticket counter first, the last person to enter is
served last. Which is why Queues are called as First in First out (FIFO) system or Last in Last out
system (LILO)
Front – The item at the front of the queue is called front item
Rear – The item at the end of the Queue is called rear item
Enqueue – Process of adding or inserting a new item in the queue is called as Enqueing
Dequeueing – Process of removing or deleting an existing item from the queue is called as
dequeueing
Size – The max size of the queue is called as size an is initialised when the queue is
created
currSize – The size of queue at any given time is notated as currSize.
C Program to Implement Queues using Arrays
#include
#define SIZE 5
rear = rear + 1;
queue[rear] = item;
printf("We have enqueued %d\n",item);
}
}
printQueue();
return 0;
}
Output:
We have enqueued 2
We have enqueued 4
We have enqueued 6
We have enqueued 8
We have dequeued : 2
We have dequeued : 4
#include
#define maxCapacity 5
printf("\n%d Successfully dequeued & changed front value which is: at pos:%d\n", item,front);
}
}
void display(){
if(rear == -1)
printf("\nQueue was Empty!!!");
else{
int i;
printf("\nQueue :\n");
int main()
{
enQueue(2);//front: a[0], rear: a[0]
enQueue(4);//front: a[0], rear: a[1]
enQueue(6);//front: a[0], rear: a[2]
enQueue(8);//front: a[0], rear: a[3]
return 0;
}
Output:
Dequeued : 4
4 Successfully dequeued & changed front value which is: at pos:2
Dequeued : 6
6 Successfully dequeued & changed front value which is: at pos:3
14 Successfully Enqueued at array pos:2
Applications in Real life scenario and Technologies
There are some real-life examples where we implement a queue. These can be,
Job scheduling.
Key board Buffers.
Request priority of users or consumers in a website or company.
Round Robin Mechanism.
and lots of things.
There are certain problems where you can make your solution optimised regarding time or
space or both with the use of a queue or more.
Any algorithm that demands for a FIFO holding of variables will be a perfect example for
applications of queue.
Also in Graph theory, you can do whole lot of things using queue, like:
Breadth First Search with the help of queue.
Topological sorts.
Finding shortest paths.
Ford Fulkerson Algorithm for Maximum flow in a network.
Level order Traversals for a Tree..
Implementing queue using linked list will not change its behavior i.e. the queue will continue
to work with FIFO architecture.
dequeue()
Check if queue is empty or not.
If queue is empty then dequeue is not possible.
Else store front in temp
And make next of front as front.
print()
Check if there is some data in the queue or not.
If the queue is empty print “No data in the queue.”
Else define a node pointer and initialize it with front.
Print data of node pointer until the next of node pointer becomes NULL.
print()
STRUCT NODE* TEMP
IF((FRONT == NULL)&&(REAR == NULL))
RETURN
ELSE
TEMP = FRONT
WHILE(TEMP)
RETURN TEMP->DATA
TEMP = TEMP->NEXT
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
};//defining linked list to implement queue
Output
Queue: 5 10 15 20 25
Queue After Dequeue: 10 15 20 25
Implementation of Queues using Stack in C is a process of creating a queue using Stacks. In this
article, we will be using a single stack for the purpose. When a single stack is used for
implementing queues recursive stack call used. This article contains in detail steps and algorithm
for all the functions of queue i.e to insert data, to delete data and to print the data.
enqueue(data)
In this enqueue function recursively call push() function.
In this push function check if the stack is full or not
If the stack is we cannot add more elements.
Else add the element at top of stack and increase the value of top by 1.
dequeue()
In this function first we will check if the queue has some element or not.
If our queue is empty then we cannot delete any element from it.
Else if there is only one element in the queue we will pop an element from the top of stack.
Else we will pop all the data from the stack until top reaches to zero and will push back the
removed data after poping the element at top=0.
print()
Initialize a for loop from zero till the top.
Print the element at every successful iteration.
In this way we can display our queue.
pop()
RETURN S[TOP–]
Algorithm for print()
FOR I=0 TO TOP
RETURN S[I]
#include<stdio.h>
#include<stdlib.h>
int dequeue ()
{
int data, res;
if (top == -1)
printf ("Queue is Empty");
else if (top == 0)
return pop ();
data = pop ();
res = dequeue ();
push (data);
return res;
int main ()
{
enqueue (5);
enqueue (10);
enqueue (15);
enqueue (20);
enqueue (25);
printf ("Queue:");
display ();
printf ("\nQueue After Dequeue:");
dequeue ();
display ();
Output
Queue: 5 10 15 20 25
Queue After Dequeue: 10 15 20 25
Algorithm:
We take two stacks say stack1 and stack2.
stack1 will be working as the main queue and stack2 will help us in reversing the order of
stack1.
For enqueue operation, we will simply push the element let say x into stack1.
For dequeue operation,
o If both the stack1 and stack2 are empty then we need to print that queue is empty.
o If stack2 is empty then we need to push all the elements from stack1 to stack2 .
o Pop the element from stack2 and return it.
#include <stdio.h>
#include<stdlib.h>
else
{
top_stack1++;
stack1[top_stack1] = data;
}
return;
}
return;
int pop_stack1 ()
{
if (top_stack1 == -1)
{
printf ("Stack1 is underflow\n");
return -1;
}
return stack1[top_stack1--];
}
int pop_stack2 ()
{
if (top_stack2 == -1)
{
printf ("Stack2 is underflow\n");
return -1;
}
return stack2[top_stack2--];
void dequeue ()
{
if (top_stack1 == -1 && top_stack2 == -1)
printf ("Queue is empty\n");
else
{
for (int i = 0; i < count; i++)
{
}
}
}
void display ()
{
if (top_stack1 == -1)
{
printf ("Queue is empty \n");
return;
}
printf ("\n");
void top ()
{
printf ("Top element of queue is %d ", stack1[0]);
}
int main ()
{
enqueue (3);
enqueue (45);
enqueue (-1);
display ();
dequeue ();
display ();
return 0;
Output:
Elements in queue : 3 45 -1
Deleted element : 3
Elements in queue : 45 -1