- 2073 Magh · 2+3 marks
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
| Operation | Array contents (index 0 → N-1) | front | rear |
|---|---|---|---|
| enqueue(10) | 10 _ _ | 0 | 0 |
| enqueue(20) | 10 20 _ | 0 | 1 |
| enqueue(30) | 10 20 30 | 0 | 2 |
| enqueue(40): rear = 2 → Queue overflow | 10 20 30 | 0 | 2 |
| dequeue() returns 10 | _ 20 30 | 1 | 2 |
| dequeue() returns 20 | _ _ 30 | 2 | 2 |
| dequeue() returns 30 | _ _ _ | 3 | 2 |
| dequeue(): front > rear → Queue underflow | _ _ _ | 3 | 2 |
- The fourth
enqueue(40)findsrear = 2 = N - 1, so overflow occurs. - After three dequeues all elements are gone (
front > rear), so the fourthdequeue()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)
Related questions
- Resolving underflow in B-tree · Data Structure and Algorithm, chapter 6
- Petrol pump queue simulation · Data Structure and Algorithm, chapter 2
- Array overflow/underflow; add two arrays · Computer Programming, chapter 7
- Thread errors and effect of pitch error · Metrology, chapter 12
- Chi-square goodness of fit: Poisson errors · Probability and Statistics, chapter 7 · asked 2×
- Why program inspections find errors · Software Engineering, chapter 7