A linear array queue advances head on dequeue and tail on enqueue. It is simple but suffers from false fullness: after a few dequeues, the front slots are unused, yet tail is at the end of the array, so new items are rejected. Solutions: shift elements (O(n)) or use a circular queue.
Remember
#define CAP 3
struct Queue { int data[CAP]; int head, tail; };
if (q->tail == CAP) { /* "full" even if head > 0 */ }
Reference
Cheat Sheet
Keep these facts handy while you work with Queue Array Implementation in C in C.
#include <stdio.h>
#define CAP 3
struct Queue { int data[CAP]; int head, tail; };
static int enqueue(struct Queue *q, int v) {
if (q->tail == CAP) return 0;
q->data[q->tail++] = v;
return 1;
}
int main(void) {
struct Queue q = {{0}, 0, 0};
for (int i = 1; i <= 3; i++)
printf("enqueue %d: %s\n", i, enqueue(&q, i) ? "ok" : "full");
printf("dequeued %d\n", q.data[q.head++]);
printf("enqueue 4: %s\n", enqueue(&q, 4) ? "ok" : "full");
printf("size=%d\n", q.tail - q.head);
return 0;
}
Output
enqueue 1: ok
enqueue 2: ok
enqueue 3: ok
dequeued 1
enqueue 4: full
size=2
How It Works
After dequeuing, slot 0 is free but tail is still 3.
The queue reports full while holding only 2 items.
A circular queue would accept 4.
Edge Cases & Pitfalls
Check these before relying on Queue Array Implementation in C in real programs.
false
False full
Linear array queues waste the slots before head.
reset
Resetting only when empty
You can reset head and tail to 0 when the queue empties, but not otherwise.
Remember
Key Takeaways
Simple but wasteful.
False full condition.
Use circular for production.
One line: A linear array queue advances head on dequeue and tail on enqueue.
Frequently Asked Questions
Dequeued slots at the front are never reused because tail only moves forward.
Use a circular queue with modulo arithmetic so indexes wrap around.
Continue with "Queue Linked List Implementation in C" next. Enqueue at the tail and dequeue at the head of a linked list. The C Queues hub lists every topic in order.