📖 Scenario: Imagine a ticket counter where people stand in a line. The first person to arrive is the first to get the ticket and leave. This is called FIFO - First In, First Out. We will create a simple queue to simulate this line.
🎯 Goal: Build a queue using an array in C that follows the FIFO principle. You will add people to the queue and remove them in the order they arrived.
📋 What You'll Learn
Create an integer array called queue with size 5
Create two integer variables front and rear to track the queue ends
Write a function enqueue to add an element to the queue
Write a function dequeue to remove an element from the queue
Print the queue elements after enqueue and dequeue operations
💡 Why This Matters
🌍 Real World
Queues are used in real life for lines at stores, printers, and task scheduling where order matters.
💼 Career
Understanding queues helps in software development for managing tasks, requests, and resources efficiently.
Progress0 / 4 steps
1
Create the queue array and initialize pointers
Create an integer array called queue with size 5. Also create two integer variables called front and rear. Initialize front to -1 and rear to -1.
DSA C
Hint
Think of front and rear as markers for the start and end of the line.
2
Add the enqueue function to add elements
Write a function called enqueue that takes an integer value and adds it to the queue. Inside the function, first check if rear is 4 (queue full). If full, print "Queue is full". Otherwise, increase rear by 1 and add value to queue[rear]. If front is -1, set it to 0.
DSA C
Hint
Remember to check if the queue is full before adding.
3
Add the dequeue function to remove elements
Write a function called dequeue that removes and returns the front element from the queue. If front is -1 or greater than rear, print "Queue is empty" and return -1. Otherwise, store queue[front] in a variable, increase front by 1, and return the stored value.
DSA C
Hint
Check if the queue is empty before removing an element.
4
Test the queue by enqueueing and dequeueing
In the main function, enqueue the values 10, 20, and 30 using enqueue. Then dequeue one value and print it with printf. Finally, print the remaining queue elements from front to rear using a for loop.
DSA C
Hint
Use a loop from front to rear to print the queue elements.
Practice
(1/5)
1. What does FIFO stand for in the context of a queue?
easy
A. First Input, First Operation
B. First In, Final Out
C. Fast Input, Fast Output
D. First In, First Out
Solution
Step 1: Understand FIFO meaning
FIFO means the first element added to the queue is the first one to be removed.
Step 2: Match the acronym
First In, First Out exactly describes this behavior.
Final Answer:
First In, First Out -> Option D
Quick Check:
FIFO = First In, First Out [OK]
Hint: Remember queue removes oldest item first [OK]
Common Mistakes:
Confusing FIFO with LIFO
Thinking FIFO means last added first removed
Mixing up acronym letters
2. Which of the following is the correct syntax to enqueue an element `x` into a queue named `q` in C?
easy
A. push(q, x);
B. enqueue(q, x);
C. q.enqueue(x);
D. q.add(x);
Solution
Step 1: Identify common C queue operation
In C, queue operations are usually functions like enqueue(queue, element).
Step 2: Check options for C style
enqueue(q, x); uses function call syntax typical in C. Others use object-oriented syntax not common in C.
Final Answer:
enqueue(q, x); -> Option B
Quick Check:
C queue enqueue = enqueue(q, x); [OK]
Hint: C uses function calls, not dot notation [OK]
Common Mistakes:
Using object-oriented syntax in C
Confusing push with enqueue
Using wrong function names
3. Consider the following C code snippet using a queue: