Queue Program in C – Array Implementation with ENQUEUE and DEQUEUE

A queue program in C implements the FIFO (First In, First Out) data structure — elements are inserted at the rear and removed from the front, just like a real queue. The two core operations are ENQUEUE (insert) and DEQUEUE (remove).

This page covers the array-based linear queue with all operations, a step-by-step trace, and an explanation of its one key limitation — wasted space after dequeuing.

Queue Operations

  • ENQUEUE: Add an element at the rear. Fails if the queue is full (overflow).
  • DEQUEUE: Remove and return the element at the front. Fails if empty (underflow).
  • PEEK: Return the front element without removing it.
  • isEmpty: True when front == -1 or front > rear.
  • isFull: True when rear == SIZE - 1.

Queue Program in C — Array Implementation

#include <stdio.h>

#define SIZE 8

int queue[SIZE];
int front = -1, rear = -1;

int isFull(void)  { return rear == SIZE - 1; }
int isEmpty(void) { return front == -1 || front > rear; }

void enqueue(int val)
{
    if (isFull()) {
        printf("Queue overflow — cannot enqueue %d.\n", val);
        return;
    }
    if (front == -1) front = 0;
    queue[++rear] = val;
    printf("Enqueued: %d\n", val);
}

int dequeue(void)
{
    int val;
    if (isEmpty()) {
        printf("Queue underflow — nothing to dequeue.\n");
        return -1;
    }
    val = queue[front++];
    if (front > rear) front = rear = -1;   /* reset when empty */
    return val;
}

int peek(void)
{
    if (isEmpty()) { printf("Queue is empty.\n"); return -1; }
    return queue[front];
}

void display(void)
{
    int i;
    if (isEmpty()) { printf("Queue is empty.\n"); return; }
    printf("Front → ");
    for (i = front; i <= rear; i++)
        printf("%d ", queue[i]);
    printf("← Rear\n");
}

int main(void)
{
    enqueue(10);
    enqueue(20);
    enqueue(30);
    enqueue(40);
    display();

    printf("Peek: %d\n", peek());
    printf("Dequeued: %d\n", dequeue());
    printf("Dequeued: %d\n", dequeue());
    display();

    return 0;
}

How to Compile and Run

gcc -Wall -o queue queue.c
./queue

Sample Output

Enqueued: 10
Enqueued: 20
Enqueued: 30
Enqueued: 40
Front → 10 20 30 40 ← Rear
Peek: 10
Dequeued: 10
Dequeued: 20
Front → 30 40 ← Rear

Step-by-Step Trace

Initial:       front=-1, rear=-1  []

enqueue(10):   front=0, rear=0   [10]
enqueue(20):   front=0, rear=1   [10, 20]
enqueue(30):   front=0, rear=2   [10, 20, 30]

dequeue():     returns 10        front=1, rear=2   [_, 20, 30]
dequeue():     returns 20        front=2, rear=2   [_, _, 30]

enqueue(40):   front=2, rear=3   [_, _, 30, 40]

Notice the _ slots at indices 0 and 1 — they are wasted after dequeuing. Even if you dequeue everything and re-enqueue, rear keeps advancing until it hits SIZE - 1, and the queue reports “full” even though the front slots are free. This is the fundamental limitation of a linear array queue.

The fix: Circular Queue — rear wraps around to index 0 when it reaches the end, reusing the freed slots.

Interactive Menu Version

#include <stdio.h>

#define SIZE 8

int queue[SIZE];
int front = -1, rear = -1;

int isFull(void)  { return rear == SIZE - 1; }
int isEmpty(void) { return front == -1 || front > rear; }

void enqueue(int val)
{
    if (isFull()) { printf("Overflow.\n"); return; }
    if (front == -1) front = 0;
    queue[++rear] = val;
}

int dequeue(void)
{
    int val;
    if (isEmpty()) { printf("Underflow.\n"); return -1; }
    val = queue[front++];
    if (front > rear) front = rear = -1;
    return val;
}

void display(void)
{
    int i;
    if (isEmpty()) { printf("Empty queue.\n"); return; }
    printf("Front → ");
    for (i = front; i <= rear; i++) printf("%d ", queue[i]);
    printf("← Rear\n");
}

int main(void)
{
    int choice, val;
    do {
        printf("\n1-Enqueue  2-Dequeue  3-Peek  4-Display  5-Exit\nOption: ");
        scanf("%d", &choice);
        switch (choice) {
        case 1:
            printf("Value: "); scanf("%d", &val);
            enqueue(val); break;
        case 2:
            val = dequeue();
            if (val != -1) printf("Dequeued: %d\n", val);
            break;
        case 3:
            if (!isEmpty()) printf("Front: %d\n", queue[front]);
            else printf("Empty.\n");
            break;
        case 4: display(); break;
        }
    } while (choice != 5);
    return 0;
}

Time and Space Complexity

Operation Time Space
ENQUEUE O(1) O(1)
DEQUEUE O(1) O(1)
PEEK O(1) O(1)
DISPLAY O(n) O(1)
Total storage O(n)

What This Program Teaches

  • The FIFO principle: insertion at rear, deletion from front — opposite to a stack
  • The reset condition: if (front > rear) front = rear = -1 — restores the “empty” sentinel after the last element is dequeued
  • The wasted-space limitation of linear queues and why circular queues exist
  • Real-world uses: CPU scheduling, printer spooling, BFS graph traversal, buffering

Related Programs


As an Amazon Associate we earn from qualifying purchases.

Recommended Book

Arrays, structs, and data structures in C are covered in The C Programming Language by Kernighan & Ritchie. Also on Amazon.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>