Skip to main content

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)

OperationArray contents (index 0 → N-1)frontrear
enqueue(10)10 _ _ _ _00
enqueue(20)10 20 _ _ _01
enqueue(30)10 20 30 _ _02
dequeue() returns 10_ 20 30 _ _12
enqueue(40)_ 20 30 40 _13
dequeue() returns 20_ _ 30 40 _23

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)

All Data Structure and Algorithm chapter 2 questions →