Queues - Array Implementation
Queues - Array Implementation
July 9, 2025 1
Queue
• Stores a set of elements in a particular order
• Queue principle: FIRST IN FIRST OUT
• = FIFO
• It means: the first element inserted is the first one
to be removed
• Example
July 9, 2025 3
Queue Represetation
July 9, 2025 4
Applications of queue
• Queues are useful for many simulations, and are also
used for some operations on graphs and trees.
• When jobs are submitted to a printer, they are
arranged in order of arrival.
• Every real-life line is a queue. For instance, lines at
ticket counters are queues, because service is first-
come first-served
July 9, 2025 5
Types of Queues
• Linear Queue
• Circular Queue
• Deques(Double Ended queue)
• Priority Queues
Operations/ADT
• enqueue
– add a new item at the rear
• dequeue
– remove a item from the front
• isEmpty
– check whether the queue is empty or not
• isFull
– check whether the queue is full or not
• size
– return the number of items in the queue
• peek
– return the front item
7
Queue Applications
• Real life examples
– Waiting in line
– Waiting on hold for tech support
• Applications related to Computer Science
– Threads
– Job scheduling (e.g. Round-Robin algorithm for
CPU allocation)
Queues Representation
• Front-Deletion-Dequeue
– Front = NULL=> Queue is empty
– Front=Front+1
• Rear-Insertion-Enqueue
– Rear=Null=>Queue is empty
– Rear=Rear+1
• Front=0,rear=0
Implementation of Linear Queue
• Using Array
• Using Linked list
Queue Using Array
• Structure can be implemented using one dimensional
array. But, queue implemented using array can store
only fixed number of data values.
}}
display() - Displays the elements of a
Queue
• Step 1: Check whether queue is EMPTY. (front == rear)
• Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set
'i = front+1'.
• Step 4: Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value is equal to rear (i <= rear)
void display()
{
if(qempty())
printf("\nQueue is Empty!!!");
else
{
int i;
printf("\nQueue elements are:\n");
for(i=Q.front; i<=Q.rear; i++)
printf("%d\t",Q.queue[i]);
Circular Queue
• Element next to the last element is the first
element.
• Front=-1, Queue=-1.
Circular Array
rear=3
• Wrapped around array
3
2
C
n-1 3 2 1 0 front=0 1 B
…… C BA
0 A
rear=3 front=0 n-1
25
EnQueue & DeQueue In Circular Array
• EnQueue • DeQueue
– rear = (rear + 1) MOD n – front = (front + 1) MOD n
rear=3 front=1
3 3
2 2
C C
1 B 1 B
0 A 0
n-1 n-1
26
do enqueue
{
Rear=(rear+1)%max,
If(front==rear)
{ “Q is full”;}
Else
{
Q[rear]=x;
Front=0;
}
}
While(op==1)
Front=-1,rear=-1;
Do Dequeue
{
Rear=(rear+1)%max,
If(front==rear)
{ “Q is empty”;}
Else
{
Q[front]=0;
Rear=0;
}
}
While(op==1)
DEQUES
• Two ends LEFT and RIGHT
• Two Variations
– Input Restricted deque
• Insertion at only one end of the list but deletion at both
the ends
– Output Restricted deque
• Deletion at only one end of the list but Insertion at
both the ends
Priority Queues
• Each element is assigned priority
• Elements are added or removed as per
priority
• Lower priority number indicates higher
priority
• Priority Queue can be implemented using
multiple queues
Priority Queue
• Two queues
– one is high priority queue
– the other is low priority queue
• Service rules:
– First serve the people in high priority queue
– If no passengers are in high priority queue, serve
the passengers in low priority queue
31
Two Queues
32