Bird
0
0
DSA Cprogramming~3 mins

Why Queue Concept and FIFO Principle in DSA C?

Choose your learning style9 modes available
The Big Idea

What if you could never lose track of who came first, no matter how many tasks pile up?

The Scenario

Imagine you are at a busy ticket counter where people line up to buy tickets. Everyone waits their turn patiently, and the first person who arrived is the first to be served.

The Problem

Now, imagine if people were served randomly or the last person to arrive was served first. It would cause confusion, unfairness, and chaos. Manually keeping track of who came first and who should be served next is slow and error-prone.

The Solution

The queue data structure follows the same rule as the ticket line: First In, First Out (FIFO). It automatically keeps track of the order, so the first element added is always the first one removed, making the process fair and organized.

Before vs After
Before
int tickets[100];
int front = 0, rear = 0;
// Manually shifting elements after serving
for (int i = 0; i < rear - 1; i++) {
    tickets[i] = tickets[i + 1];
}
rear--;
After
typedef struct {
    int items[100];
    int front, rear;
} Queue;

void enqueue(Queue* queue, int value) {
    queue->items[queue->rear++] = value;
}

int dequeue(Queue* queue) {
    return queue->items[queue->front++];
}
What It Enables

Queues enable smooth, fair, and efficient processing of tasks or data in the exact order they arrive.

Real Life Example

Queues are used in printer job management where print requests are handled one by one in the order they were sent.

Key Takeaways

Queues follow the First In, First Out (FIFO) principle.

They automate fair and orderly processing of elements.

Queues prevent manual errors and confusion in managing order.