0
0
CHow-ToBeginner · 4 min read

How to Implement Queue in C: Simple Guide with Example

To implement a queue in C, you can use either an array or a linked list to store elements. The queue follows FIFO (First In First Out) principle, where you add elements at the rear and remove from the front using functions like enqueue and dequeue.
📐

Syntax

A queue in C can be implemented using a struct that holds the data array or linked nodes, and two indices or pointers: front and rear. The main operations are:

  • enqueue(): Add an element at the rear.
  • dequeue(): Remove an element from the front.
  • isEmpty(): Check if the queue is empty.
  • isFull(): Check if the queue is full (for array implementation).
c
typedef struct {
    int items[SIZE];
    int front, rear;
} Queue;

void enqueue(Queue *q, int value);
int dequeue(Queue *q);
int isEmpty(Queue *q);
int isFull(Queue *q);
💻

Example

This example shows a queue implemented using an array with fixed size. It demonstrates adding and removing elements while maintaining the FIFO order.

c
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5

typedef struct {
    int items[SIZE];
    int front, rear;
} Queue;

void initQueue(Queue *q) {
    q->front = -1;
    q->rear = -1;
}

int isFull(Queue *q) {
    return q->rear == SIZE - 1;
}

int isEmpty(Queue *q) {
    return q->front == -1 || q->front > q->rear;
}

void enqueue(Queue *q, int value) {
    if (isFull(q)) {
        printf("Queue is full\n");
        return;
    }
    if (q->front == -1) q->front = 0;
    q->rear++;
    q->items[q->rear] = value;
    printf("Enqueued: %d\n", value);
}

int dequeue(Queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty\n");
        return -1;
    }
    int item = q->items[q->front];
    q->front++;
    if (q->front > q->rear) {
        q->front = q->rear = -1; // Reset queue
    }
    return item;
}

int main() {
    Queue q;
    initQueue(&q);

    enqueue(&q, 10);
    enqueue(&q, 20);
    enqueue(&q, 30);

    printf("Dequeued: %d\n", dequeue(&q));
    printf("Dequeued: %d\n", dequeue(&q));

    enqueue(&q, 40);
    enqueue(&q, 50);
    enqueue(&q, 60); // Should show full

    while (!isEmpty(&q)) {
        printf("Dequeued: %d\n", dequeue(&q));
    }

    return 0;
}
Output
Enqueued: 10 Enqueued: 20 Enqueued: 30 Dequeued: 10 Dequeued: 20 Enqueued: 40 Enqueued: 50 Queue is full Dequeued: 30 Dequeued: 40 Dequeued: 50
⚠️

Common Pitfalls

Common mistakes when implementing a queue in C include:

  • Not properly updating front and rear indices, causing incorrect enqueue or dequeue behavior.
  • Not resetting the queue when it becomes empty, leading to invalid states.
  • Ignoring the queue full condition in array implementations, which causes overflow.
  • Confusing front and rear positions.
c
/* Wrong: Not resetting front and rear after dequeue empties queue */
if (q->front > q->rear) {
    // Missing reset here causes errors
}

/* Right: Reset front and rear to -1 when queue is empty */
if (q->front > q->rear) {
    q->front = q->rear = -1;
}
📊

Quick Reference

Remember these key points when implementing a queue in C:

  • FIFO order: Add at rear, remove from front.
  • Indices: Use front and rear to track positions.
  • Check full/empty: Prevent overflow and underflow.
  • Reset: Reset indices when queue becomes empty.

Key Takeaways

Use a struct with front and rear indices to track queue state.
Always check if the queue is full before enqueue and empty before dequeue.
Reset front and rear to -1 when the queue becomes empty to avoid errors.
Implement enqueue to add at rear and dequeue to remove from front following FIFO.
Handle edge cases like overflow and underflow carefully to keep queue stable.