0
0
CHow-ToBeginner · 4 min read

How to Implement Queue Using Array in C: Simple Guide

To implement a queue using an array in C, create an array with two pointers: front and rear to track the start and end of the queue. Use these pointers to add elements at the rear and remove from the front, managing overflow and underflow conditions.
📐

Syntax

A queue using an array in C typically involves:

  • An array to store elements.
  • Two integer variables front and rear to track the queue's start and end.
  • Functions to enqueue (add) and dequeue (remove) elements.
  • Checks for overflow (queue full) and underflow (queue empty).
c
int queue[MAX_SIZE];
int front = -1;
int rear = -1;

// Enqueue function
void enqueue(int value) {
    if (rear == MAX_SIZE - 1) {
        // Queue is full
    } else {
        if (front == -1) front = 0;
        rear++;
        queue[rear] = value;
    }
}

// Dequeue function
int dequeue() {
    if (front == -1 || front > rear) {
        // Queue is empty
        return -1; // or error code
    } else {
        int value = queue[front];
        front++;
        if (front > rear) {
            front = rear = -1; // Reset queue when empty
        }
        return value;
    }
}
💻

Example

This example shows a simple queue implementation using an array with enqueue and dequeue operations, including checks for full and empty queue.

c
#include <stdio.h>
#define MAX_SIZE 5

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

void enqueue(int value) {
    if (rear == MAX_SIZE - 1) {
        printf("Queue is full. Cannot enqueue %d\n", value);
    } else {
        if (front == -1) front = 0;
        rear++;
        queue[rear] = value;
        printf("Enqueued: %d\n", value);
    }
}

int dequeue() {
    if (front == -1 || front > rear) {
        printf("Queue is empty. Cannot dequeue.\n");
        return -1;
    } else {
        int value = queue[front];
        front++;
        printf("Dequeued: %d\n", value);
        if (front > rear) {
            front = rear = -1; // Reset queue when empty
        }
        return value;
    }
}

int main() {
    enqueue(10);
    enqueue(20);
    enqueue(30);
    dequeue();
    dequeue();
    dequeue();
    dequeue(); // extra dequeue to show empty
    enqueue(40);
    enqueue(50);
    enqueue(60);
    enqueue(70);
    enqueue(80);
    enqueue(90); // extra enqueue to show full
    return 0;
}
Output
Enqueued: 10 Enqueued: 20 Enqueued: 30 Dequeued: 10 Dequeued: 20 Dequeued: 30 Queue is empty. Cannot dequeue. Enqueued: 40 Enqueued: 50 Enqueued: 60 Enqueued: 70 Enqueued: 80 Queue is full. Cannot enqueue 90
⚠️

Common Pitfalls

Common mistakes when implementing a queue using an array include:

  • Not updating front and rear correctly, causing incorrect queue state.
  • Ignoring overflow (queue full) and underflow (queue empty) conditions.
  • Not resetting front and rear when queue becomes empty, leading to errors on further operations.
  • Using a simple array without wrapping around wastes space after some dequeues (fixed by circular queue).
c
/* Wrong: Not checking overflow */
void enqueue_wrong(int value) {
    rear++;
    queue[rear] = value; // May write outside array bounds
}

/* Right: Check overflow before enqueue */
void enqueue_right(int value) {
    if (rear == MAX_SIZE - 1) {
        printf("Queue is full.\n");
        return;
    }
    if (front == -1) front = 0;
    rear++;
    queue[rear] = value;
}
📊

Quick Reference

  • front: Index of first element in queue or -1 if empty.
  • rear: Index of last element in queue or -1 if empty.
  • Enqueue: Add element at rear if not full.
  • Dequeue: Remove element from front if not empty.
  • Overflow: When rear reaches max size - 1.
  • Underflow: When front is -1 or front > rear.

Key Takeaways

Use two pointers, front and rear, to track the queue's start and end in the array.
Always check for overflow before enqueue and underflow before dequeue to avoid errors.
Reset front and rear properly when the queue becomes empty to maintain correct state.
A simple array queue wastes space after dequeues; consider circular queue for efficiency.
Implement enqueue by adding at rear and dequeue by removing from front.