You are on page 1of 4

PRESIDENCY UNIVERSITY

Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering

Subject: CSE2001 - Data Structures & Algorithms Semester: III


Date: 26/09/2023

Implementation of Queue using Arrays

Linear data structure: Queue (FIFO – First In First Out or LILO - Last in Last Out)
Assume Queue size=5
1. Inserting elements is called as “Enqueue” operation in Queue
Enqueue is done using “rear” variable.

2. Deleting elements is called as “Dequeue” operation in Queue


Dequeue is done using “front” variable.

Note: Queue principle – Insertion is done from one end and Deletion is done at another end.

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
1. Initially Queue will be empty 0 1 2 3 4
2. front and rear varaibles are
initialised with value -1
indicating that Queue is empty front=-1, rear=-1
1. Insertion is done through rear 0 1 2 3 4
end and deletion is done enqueue(10) 10
through front end.
2. To insert value first increment
rear & front variable and insert front=0
value into queue.
3. Invoke enqueue(10) rear=0
4. front=0, rear=0
1. To insert second element, 0 1 2 3 4
increment rear variable and enqueue(20) 10 20
then insert element.
2. Invoke enqueue(20) front=0

rear=1
1. To insert third element, 0 1 2 3 4
increment rear variable and enqueue(30) 10 20 30
then insert element.
2. Invoke enqueue(30) front=0

rear=2
1. To insert fourth element, enqueue(40) 0 1 2 3 4
increment rear variable and 10 20 30 40
then insert element.
2. Invoke enqueue(40) front=0

rear=3
1. To insert fifth element, 0 1 2 3 4
increment rear variable and
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 2
then insert element. Enqueue(50) 10 20 30 40 50
2. Invoke enqueue(50)
front=0

rear=4
1. To insert sixth element, Invoke Queue Overflow Condition
enqueue(60). Since the Queue
is full element will not be
inserted and such condition is
called as “Queue Overflow”.
1. Now if dequeue() is invoked, 0 1 2 3 4
first element will be deleted 20 30 40 50
from Queue. dequeue()
2. front variable is incremented
by 1. front=1

rear=4
1. Now if dequeue() is invoked, 0 1 2 3 4
second element will be deleted 30 40 50
from Queue. dequeue()
2. front variable is incremented
by 1. front=2

rear=4
1. Now if dequeue() is invoked, 0 1 2 3 4
third element will be deleted 40 50
from Queue. dequeue()
2. front variable is incremented
by 1. front=3

rear=4
1. Now if enqueue(60) is 0 1 2 3 4

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 3
invoked, enough if 3 elements 40 50
can be inserted into Queue, enqueue(60)
new values cannot be added as
rear=4 which indicates Queue front=3
Overflow.
2. This is the disadvantage of rear=4
Queue. Queue Overflow
3. Such drawback is overcome by
circular Queue.

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 4

You might also like