🔴 Advanced  ·  Lesson 40

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

OperationWhat it does
enqueue(x)Add item x at the rear
dequeue()Remove the item at the front
frontIndex of the next item to remove
rearIndex of the last item added

Full and empty conditions

  • Empty — no items to remove (front is greater than rear, or both start at -1/0 depending on the style).
  • Fullrear has reached the last index, so there is no room to enqueue.
⚠️ Always check

Check for full before every enqueue and for empty before every dequeue, just like overflow/underflow in a stack.

Array implementation

C Language
#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;
}
Output:
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

PointQueueStack
RuleFIFO (first in, first out)LIFO (last in, first out)
Add atRearTop
Remove fromFrontTop
Real pictureLine of peoplePile 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, rear or the count after each operation.
🏋️ Practice

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.
  • enqueue adds at the rear; dequeue removes from the front.
  • Track positions with front and rear (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?
A queue is a data structure that follows the FIFO rule — First In, First Out. The item added first is the first one removed, exactly like people standing in a line: whoever arrives first is served first. In C it is often built with an array plus two markers, 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?
A stack is LIFO (last in, first out) and works from one end only. A queue is FIFO (first in, first out) and works from two ends — add at the rear, remove from the front. A stack is like a pile of plates; a queue is like a line of people.
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?
In a basic array queue, once items are dequeued the 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.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.