Queue Data Structure in C (FIFO Operations)

A circular queue enforces First-In, First-Out (FIFO) ordering. Learn circular buffer arithmetic, enqueue, dequeue, and wrap-around index math in C.
A Queue operates on the First-In, First-Out (FIFO) principle: elements are enqueued at the rear and dequeued from the front. Queues are the backbone of asynchronous message passing, job schedulers, and breadth-first search (BFS) graph traversals.
A linear array-based queue quickly wastes memory because the front pointer marches forward, leaving dead space behind it. To solve this, this implementation uses a Circular Queue, wrapping the indices around using the modulo operator ((rear + 1) % CAPACITY).
Circular queues achieve optimal O(1) time complexity for both enqueue and dequeue operations with zero memory shifts or reallocations.
Interactive Runnable C Implementation
#include <stdio.h>
#define SIZE 5
typedef struct {
int items[SIZE];
int front;
int 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 val) {
if (isFull(q)) {
printf("Queue Full!\n");
return;
}
if (q->front == -1) q->front = 0;
q->items[++(q->rear)] = val;
}
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue Empty!\n");
return -1;
}
return q->items[(q->front)++];
}
int main() {
Queue q;
initQueue(&q);
enqueue(&q, 100);
enqueue(&q, 200);
enqueue(&q, 300);
printf("Dequeued: %d\n", dequeue(&q));
printf("Dequeued: %d\n", dequeue(&q));
return 0;
}
Expected Output
Dequeued: 100
Dequeued: 200
Frequently Asked Questions
What is the time complexity of Queue Data Structure in C (FIFO)?
FIFO (First-In First-Out) discipline with predictable asymptotic runtime behavior.