Skip to main content

Queue overflow and underflow errors

Short answer

Queue overflow occurs when inserting into a full queue (no space), e.g., enqueue(40) on a size‑3 linear queue already holding 10,20,30. Queue underflow occurs when deleting from an empty queue, e.g., dequeue after all three were removed.

When do you get error message "Queue overflow and Queue underflow"? Explain with example.

A queue is a linear data structure in which insertion takes place at one end, the rear, and deletion at the other end, the front. The element inserted first is removed first, so it follows FIFO (First In First Out).

Queue overflow

Occurs when we try to insert into a full queue, i.e. no space is left. For a linear queue of size N the test is rear == N - 1; for a circular queue it is (rear + 1) % N == front. The element is not inserted and the message "Queue overflow" is shown.

Queue underflow

Occurs when we try to delete from an empty queue, i.e. no element exists. For a linear queue the test is front > rear; for a circular queue it is front == -1. The message "Queue underflow" is shown.

Example: linear queue of size 3

OperationArray contents (index 0 → N-1)frontrear
enqueue(10)10 _ _00
enqueue(20)10 20 _01
enqueue(30)10 20 3002
enqueue(40): rear = 2 → Queue overflow10 20 3002
dequeue() returns 10_ 20 3012
dequeue() returns 20_ _ 3022
dequeue() returns 30_ _ _32
dequeue(): front > rear → Queue underflow_ _ _32
  • The fourth enqueue(40) finds rear = 2 = N - 1, so overflow occurs.
  • After three dequeues all elements are gone (front > rear), so the fourth dequeue() causes underflow.

Both checks must be made before changing front or rear, otherwise data outside the array would be read or written.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 2 questions →