Queue using C
What is a queue? (FIFO)
A queue is a data structure that follows one simple rule: FIFO — First In, First Out. The item that goes in first is the first to come out.
Think of a line at a ticket counter. The person who arrives first is served first; new people join at the back. A queue in C works exactly like that line — you add at one end and remove from the other. (Compare this with the stack in C, which is LIFO.)
The core operations
| Operation | What it does |
|---|---|
enqueue(x) | Add item x at the rear |
dequeue() | Remove the item at the front |
front | Index of the next item to remove |
rear | Index of the last item added |
Full and empty conditions
- Empty — no items to remove (
frontis greater thanrear, or both start at -1/0 depending on the style). - Full —
rearhas reached the last index, so there is no room to enqueue.
Check for full before every enqueue and for empty before every dequeue, just like overflow/underflow in a stack.
Array implementation
#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int front = 0, rear = -1, count = 0;
void enqueue(int x) {
if (count == SIZE) { printf("Queue full\n"); return; }
rear = (rear + 1);
queue[rear] = x;
count++;
}
int dequeue() {
if (count == 0) { printf("Queue empty\n"); return -1; }
int val = queue[front];
front++;
count--;
return val;
}
int main() {
enqueue(10); enqueue(20); enqueue(30);
printf("Removed: %d\n", dequeue()); // 10 (first in)
printf("Removed: %d\n", dequeue()); // 20
return 0;
}Removed: 10
Removed: 20
Notice 10 came out first because it went in first — that is FIFO, the opposite of a stack.
Queue vs stack
| Point | Queue | Stack |
|---|---|---|
| Rule | FIFO (first in, first out) | LIFO (last in, first out) |
| Add at | Rear | Top |
| Remove from | Front | Top |
| Real picture | Line of people | Pile of plates |
Common mistakes
- Removing from the wrong end — a queue removes from the front, not the rear.
- Not checking full before enqueue or empty before dequeue.
- Ignoring wasted space in a simple array queue (use a circular queue to fix it).
- Forgetting to update
front,rearor the count after each operation.
Extend this into a circular queue so the freed front slots get reused: when rear reaches the end, wrap it back to 0 using (rear + 1) % SIZE. Test by filling, removing a few, and adding again.
Summary
- A queue follows FIFO — the first item in is the first out.
enqueueadds at the rear;dequeueremoves from the front.- Track positions with
frontandrear(and often a count). - Check full before enqueue and empty before dequeue.
- A circular queue reuses freed space; a stack is the LIFO counterpart.
Frequently Asked Questions
What is a queue in C?
front and rear.What are enqueue and dequeue?
enqueue adds a new item at the rear (back) of the queue, and dequeue removes an item from the front. Adding at one end and removing from the other is what gives a queue its First In, First Out behaviour.What is the difference between a queue and a stack?
What do front and rear mean in a queue?
front is the index of the item that will be removed next, and rear is the index of the item most recently added. As you enqueue, rear moves forward; as you dequeue, front moves forward.Why does a simple array queue waste space?
front keeps moving forward and those early slots are never reused, so the queue can appear full even with empty space at the start. A circular queue solves this by wrapping the indices back around to the beginning.