Topic Wise Lesson Plane
Operations on a Queue
In array representation, a queue is implemented using a fixed-size array and two variables:
-
FRONT → points to the first element of the queue
-
REAR → points to the last element of the queue
Initial State
FRONT = -1
REAR = -1
This indicates the queue is empty.
Basic Operations
1. ENQUEUE (Insertion)
Adds an element at the rear end of the queue.
Steps:
-
Check if
REAR == size - 1
→ If yes, Queue Overflow -
If
FRONT == -1, setFRONT = 0 -
Increment
REAR -
Insert element at
queue[REAR]
Algorithm:
ENQUEUE(queue, size, item):
if REAR == size - 1:
print "Queue Overflow"
else:
if FRONT == -1:
FRONT = 0
REAR = REAR + 1
queue[REAR] = item
2. DEQUEUE (Deletion)
Removes an element from the front end of the queue.
Steps:
-
Check if
FRONT == -1orFRONT > REAR
→ If yes, Queue Underflow -
Remove element at
queue[FRONT] -
Increment
FRONT -
If
FRONT > REAR, reset:
Algorithm:
DEQUEUE(queue):
if FRONT == -1 or FRONT > REAR:
print "Queue Underflow"
else:
item = queue[FRONT]
FRONT = FRONT + 1
if FRONT > REAR:
FRONT = REAR = -1
return item
Example
Queue size = 5
ENQUEUE(10)
ENQUEUE(20)
ENQUEUE(30)
Array:
Index: 0 1 2 3 4
Queue: 10 20 30 _ _
FRONT → 0
REAR → 2
Now:
DEQUEUE() → removes 10
Updated queue:
Index: 0 1 2 3 4
Queue: _ 20 30 _ _
FRONT → 1
REAR → 2
Advantages
-
Simple implementation
-
Fast operations (O(1))
Disadvantages
-
Fixed size
-
Wastage of memory
-
False overflow problem