Queue
Data Structures O(1) enqueue/dequeue O(n)FIFO container implemented as a circular buffer: enqueue at the rear, dequeue from the front.
Pick an operation to walk through the code.
InvariantItems leave from `front` and arrive at `rear`, both wrapping around, so order is preserved with no shifting.
queue.c
#define MAX 100typedef struct {int items[MAX];int front, rear, size;} Queue;void enqueue(Queue *q, int value) {if (q->size == MAX) return; /* full */q->rear = (q->rear + 1) % MAX;q->items[q->rear] = value;q->size++;}int dequeue(Queue *q) {if (q->size == 0) return -1; /* empty */int value = q->items[q->front];q->front = (q->front + 1) % MAX;q->size--;return value;}
How Queue works
First in, first out. Enqueue at the rear, dequeue from the front; a circular buffer wraps both indices around a fixed array so no element ever moves.
When to use it
Task scheduling, BFS, buffering between producers and consumers.