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:

  1. Check if REAR == size - 1
    → If yes, Queue Overflow

  2. If FRONT == -1, set FRONT = 0

  3. Increment REAR

  4. 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:

  1. Check if FRONT == -1 or FRONT > REAR
    → If yes, Queue Underflow

  2. Remove element at queue[FRONT]

  3. Increment FRONT

  4. If FRONT > REAR, reset:

    FRONT = REAR = -1

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

You have completed 0% of the lesson
0%