A queue is like a line at a ticket counter: people join at the back and are served at the front.

Definition

A queue is a linear data structure in which elements are inserted at one end, the rear, and deleted at the other end, the front. The first element inserted is the first one removed, so a queue follows the FIFO (First In, First Out) principle. Insertion is called enqueue and deletion dequeue. Other operations are peek (front), isEmpty and isFull. Inserting into a full queue is overflow, and deleting from an empty queue is underflow.

Definition: Queue. A linear data structure in which elements are inserted at the rear and removed from the front, following the First In, First Out (FIFO) order.

Array Representation of a Simple Queue

A queue is held in an array q[MAX] with two indices, front and rear, both −1 at the start. Enqueue: if rear = MAX − 1, report overflow; otherwise, if the queue is empty set front = 0, then increment rear and store the item at q[rear]. Dequeue: if front = −1 or front > rear, report underflow; otherwise return q[front] and increment front. Both are O(1). The weakness of this linear queue is that after some dequeues, the space at the front is wasted: rear may reach MAX − 1 and report overflow even though the front cells are free. The circular queue solves this.

Linked List Representation

A queue can be a linked list with two pointers, front to the first node and rear to the last. Enqueue adds a node after rear and moves rear to it. Dequeue removes the node at front and moves front to the next node. If the queue becomes empty, both become NULL. Both operations are O(1), and the queue grows as needed.

Table 3.5: Stack and queue

Feature Stack Queue
Order LIFO FIFO
Insert at Top Rear
Delete at Top Front
Example Pile of plates Line at a counter

Example 3.5: Enqueue and dequeue

Problem: In a queue of size 5, enqueue 10, 20, 30, dequeue twice, then enqueue 40, 50, 60. What happens?

Solution:

  1. After three enqueues: front = 0, rear = 2, queue 10 20 30.
  2. Two dequeues remove 10 and 20: front = 2.
  3. Enqueue 40 and 50: rear = 4, queue 30 40 50.
  4. Enqueue 60: rear = 4 = MAX − 1, so overflow, although cells 0 and 1 are free.

Result: The linear queue wastes space and reports overflow early.

#include <stdio.h>
#define MAX 5
int q[MAX], front = -1, rear = -1;
void enq(int v) {
    if (rear == MAX - 1) { printf("Overflow on %d\n", v); return; }
    if (front == -1) front = 0;
    q[++rear] = v;
}
int deq(void) {
    if (front == -1 || front > rear) {
        printf("Underflow\n"); return -1;
    }
    return q[front++];
}
int main(void) {
    enq(10); enq(20); enq(30);
    int x = deq(), y = deq();
    printf("%d %d dequeued\n", x, y);
    enq(40); enq(50); enq(60);
    for (int i = front; i <= rear; i++) printf("%d ", q[i]);
    printf("\n");
    return 0;
}

Output

10 20 dequeued
Overflow on 60
30 40 50

Common mistakes

  • Inserting at the front or deleting at the rear, which breaks FIFO.
  • Testing only rear = MAX − 1 for fullness in a linear queue, which wastes space.
  • Not resetting front and rear when the queue becomes empty.

Key takeaways

  • A queue is FIFO: insert at the rear, delete at the front.
  • An array queue uses front and rear indices.
  • A linear queue wastes space at the front.
  • A linked queue uses front and rear pointers, with O(1) operations.

Practice questions

  1. What does FIFO mean?
  2. What is the problem with a linear array queue?
  3. Where are elements inserted in a queue?

Answers to the practice questions

  1. First In, First Out: the first element inserted is the first removed.
  2. Space freed at the front cannot be reused, so overflow may be reported early.
  3. At the rear.

Sources

  • Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit III