MCS-208 · Unit 5

Queues

Section 2 of 8

Block 2 · Unit 5Queues9 min · Section 2 of 8

5.2Queue Implementation Using Arrays

Covers IGNOU MCS-208 §5.3, 5.3.1

The most direct way to store a queue is an array with two indices: front for the next item to leave and rear for the last item that arrived. This page builds that “linear” queue and shows its weakness, which the circular queue later fixes.

The representation

  • front = index of the first item; rear = index of the last item.
  • Empty queue: front = 0, rear = -1 (so front > rear).
  • Add: rear = rear + 1, then store at element[rear].
  • Delete: read element[front], then front = front + 1.
  • The queue cannot grow when rear == QUEUE_LENGTH - 1.

This drift is the main problem of the linear array queue: every element moves the queue’s “window” to the right, so space at the start of the array is wasted. Two fixes exist: shift all elements to the start (costs O(n)O(n) per deletion), or wrap around to the start of the array, which is the circular queue later in this unit.

Key takeaways

  • Array queue: front = first item, rear = last item; empty when front > rear.
  • Add: check full, ++rear, store. Delete: check empty, read element[front], front++.
  • Both operations are O(1)O(1).
  • Space freed at the start is wasted in a linear queue; the circular queue solves this.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

A linear queue has length 6, front = 3 and rear = 5. How many items does it hold, and can a new item be added?

Show answer

Answer: B. 3 items; no

Indices 3, 4 and 5 hold items (5 − 3 + 1 = 3). rear is already the last index, so a linear queue cannot add more, although cells 0–2 are free.

Question 2 · multiple choice

With front = 0 and rear = -1 for an empty queue, which test means the queue is empty?

Show answer

Answer: C. front > rear

Each delete moves front past rear when the last item leaves. front == rear means one item is left.

Question 3 · multiple choice

What does this program print?

C · q5-linear.c
#include <stdio.h>

int main(void)
{
    int q[5], front = 0, rear = -1;
    q[++rear] = 3;
    q[++rear] = 6;
    q[++rear] = 9;
    int a = q[front++];
    q[++rear] = 12;
    int b = q[front++];
    printf("%d %d front=%d rear=%d\n", a, b, front, rear);
    return 0;
}
Show answer

Answer: A. 3 6 front=2 rear=3

Items leave in the order 3, 6. Both indices only increase: front is 2 after two deletions and rear is 3 after four additions. “9 12” would be stack (LIFO) behaviour.

Question 4 · multiple choice

What is the main drawback of the linear array queue?

Show answer

Answer: D. Cells freed at the front cannot be reused, so the queue can report “full” while space is free.

Both indices move only forward. Shifting the elements would fix it but makes deletion O(n)O(n); the circular queue fixes it in O(1)O(1).

Question 5 · multiple choice

In a correct add, when should the overflow check be done?

Show answer

Answer: A. Before rear is changed

If rear is incremented first and the queue is full, rear points outside the array even though the item is rejected.

Question 6 · short answer

Write C functions to add and delete an item in a linear array queue, with overflow and underflow checks.

Show worked answer

#define N 50 int q[N], front = 0, rear = -1; int add(int x) { if (rear == N - 1) { printf("Queue overflow\n"); return 0; } q[++rear] = x; return 1; } int delete_front(int *x) { if (front > rear) { printf("Queue empty\n"); return 0; } *x = q[front++]; if (front > rear) { front = 0; rear = -1; } /* reset when empty */ return 1; } Both are O(1)O(1).

↑ ↓ move↵ openTab switch type