Skip to content

Computer Science · Ch 4 — Queue

Operations on Queue

4.2

Operations on Queue

The queue follows the First In, First Out (FIFO) principle. This means the element that has been in the queue the longest is the first one to be removed. To work with a queue in this way, we need two primary operations: one to add data and one to remove data.

ENQUEUE inserts a new element at the rear end of the queue. You can keep adding elements as long as there is space available in the queue's memory. If you try to insert an element when the queue is already full, the program will raise an exception called Overflow.

DEQUEUE removes one element at a time from the front of the queue. You can keep removing elements until the queue becomes empty. If you try to delete an element from an empty queue, the program will raise an exception called Underflow.

To perform enqueue and dequeue operations efficiently and safely, three supporting operations are also required.

IS EMPTY checks whether the queue has any elements or not. This is used before performing a dequeue operation to avoid the Underflow exception. If the queue is empty, you know not to attempt a removal.

PEEK allows you to view the element at the front of the queue without removing it. This is useful when you need to know what the next element to be processed is, but you are not ready to dequeue it yet.

IS FULL checks whether any more elements can be added to the queue. This is used before performing an enqueue operation to avoid the Overflow exception. If the queue is full, you know not to attempt an insertion.

The textbook illustrates these operations with a simple queue containing alphabets. In the figure, the front of the queue is on the left and the rear is on the right. The following table shows the status of the queue after each operation.

Operation performedStatus of queue after operation
enqueue(z)Z (Front and Rear)
enqueue(x)Z X (Front at Z, Rear at X)
enqueue(c)Z X C (Front at Z, Rear at C)
Figure 4.3Various Stages of Stack Operations
Fig. 4.3 — Various Stages of Stack Operations

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

This traces a simple queue of letters through a sequence of enqueue and dequeue operations, tracking the FRONT (F) and REAR (R) pointers at each step.

enqueue(z) adds Z — with only one element, F and R both point at it. enqueue(x) adds X at the rear, so now F is at Z (the oldest) and R has moved to X (the newest). enqueue(c) adds C, pushing R further along.

Then dequeue() removes whatever is at the FRONT — Z — leaving X and C, with F now pointing at X. The pattern repeats: enqueue always adds at the rear (R moves forward), dequeue always removes from the front (F moves forward) — elements leave in exactly the order they arrived, never out of turn. …

Table 4.1Various Stages of a Simple Queue (Figure 4.3)
Operation performedStatus of queue after operation (FRONT -> REAR)
enqueue(z)Z (FRONT and REAR both at Z)
enqueue(x)Z, X (FRONT = Z, REAR = X)
enqueue(c)Z, X, C (FRONT = Z, REAR = C)
dequeue()X, C (FRONT = X, REAR = C)
enqueue(v)X, C, V (FRONT = X, REAR = V)