Three different ways to implement a Queue (FIFO) data structure in C, written while studying and preparing to teach the topic.
The most basic implementation: a fixed-size array with front and rear indices that only move forward.
Demonstrates the false overflow problem — once rear reaches the end of the array, no more elements can be added even if slots at the front have been freed by dequeue().
Fixes the false overflow problem using modular arithmetic: rear = (rear + 1) % CAPACITY.
Once the end of the array is reached, the index wraps back around to 0, so freed slots at the front are reused. Still fixed capacity, but no wasted memory.
Uses dynamically allocated nodes (malloc/free) instead of a fixed-size array.
No capacity limit and no false overflow, at the cost of extra memory per node (a pointer) and the responsibility of managing allocation/deallocation correctly.
Each file is self-contained and can be compiled independently:
gcc -Wall -o array_queue array_queue.c && ./array_queue
gcc -Wall -o circular_queue circular_queue.c && ./circular_queue
gcc -Wall -o linked_list_queue linked_list_queue.c && ./linked_list_queue| Array (simple) | Circular | Linked List | |
|---|---|---|---|
| enqueue / dequeue | O(1) | O(1) | O(1) |
| Capacity | Fixed | Fixed | Dynamic |
| False overflow | Yes | No | No |
| malloc / free needed | No | No | Yes |