- Asked 2/14
- 2074 Bhadra · 1+4 marks
- 2070 Bhadra · 1+4 marks
Queue; enqueue and dequeue operations
Short answer
A queue is a FIFO linear structure with insertion at the rear and deletion at the front. Enqueue adds an element by incrementing rear and placing the element; dequeue removes and returns the front element, then increments front. Example: starting empty, enqueue 10,20,30; dequeue returns 10, then front moves to index 1.
Define queue. Explain enqueue and dequeue operation 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).
Enqueue (insert at rear) and dequeue (delete from front)
Algorithm ENQUEUE(Q, N, x) /* array Q[0..N-1], front = 0, rear = -1 initially */
1. if rear == N - 1:
print "Queue overflow"; return
2. rear = rear + 1
3. Q[rear] = x
Algorithm DEQUEUE(Q)
1. if front > rear:
print "Queue underflow"; return
2. x = Q[front]
3. front = front + 1
4. return x
Example (N = 5)
| 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 |
| dequeue() returns 10 | _ 20 30 _ _ | 1 | 2 |
| enqueue(40) | _ 20 30 40 _ | 1 | 3 |
| dequeue() returns 20 | _ _ 30 40 _ | 2 | 3 |
front moves right on every dequeue and rear moves right on every enqueue; the queue is empty when front > rear and full when rear = N - 1.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Enqueue and dequeue in circular queue · Data Structure and Algorithm, chapter 2 · asked 2×
- Circular queue with display operation · Data Structure and Algorithm, chapter 2
- Linear and circular queue operations · Data Structure and Algorithm, chapter 2