📖 Scenario: Imagine a line at a coffee shop where customers wait their turn. The first customer to arrive is the first to be served. This is called FIFO - First In, First Out. We will create a queue to simulate this line.
🎯 Goal: Build a simple queue using a list to add customers in order and remove them in the same order, demonstrating the FIFO principle.
📋 What You'll Learn
Create a queue using a std::vector<std::string> named queue with initial customers
Add a new customer to the end of the queue
Remove the first customer from the queue to simulate serving
Print the queue after serving to show the order of remaining customers
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like in banks, supermarkets, or call centers, ensuring fairness by serving in order.
💼 Career
Understanding queues is important for software jobs involving task scheduling, resource management, and handling requests in the order they arrive.
Progress0 / 4 steps
1
Create the initial queue
Create a std::vector<std::string> called queue with these exact customers in order: "Alice", "Bob", "Charlie".
DSA C++
Hint
Use std::vector<std::string> queue = {"Alice", "Bob", "Charlie"}; to create the queue.
2
Add a new customer to the queue
Add a new customer named "Diana" to the end of the queue using the push_back method.
DSA C++
Hint
Use queue.push_back("Diana"); to add Diana at the end.
3
Serve the first customer
Remove the first customer from the queue to simulate serving them. Use queue.erase(queue.begin()) to remove the first element.
DSA C++
Hint
Use queue.erase(queue.begin()); to remove the first customer.
4
Print the queue after serving
Use a for loop with variable customer to iterate over queue and print each customer followed by " -> ". After the loop, print "null" to show the end of the queue.
DSA C++
Hint
Use a for loop to print each customer followed by " -> ", then print "null".
Practice
(1/5)
1. What does the FIFO principle mean in the context of a queue?
easy
A. Last In, First Out - the last element added is the last removed
B. First In, First Out - the last element added is the first removed
C. Last In, First Out - the first element added is the last removed
D. First In, First Out - the first element added is the first removed
Solution
Step 1: Understand FIFO principle
FIFO means the first element that enters the queue is the first one to leave.
Step 2: Match FIFO with queue behavior
Queues follow FIFO, so the first added element is removed first.
Final Answer:
First In, First Out - the first element added is the first removed -> Option D
Quick Check:
FIFO = First In, First Out [OK]
Hint: Remember queue = line; first come, first served [OK]
Common Mistakes:
Confusing FIFO with LIFO
Thinking last added is removed first
Mixing queue with stack behavior
2. Which of the following is the correct way to declare a queue of integers in C++ using STL?
easy
A. queue<int> q();
B. queue q<int>;
C. queue<int> q;
D. queue<int> q[];
Solution
Step 1: Recall STL queue syntax
The correct syntax to declare a queue of integers is queue<int> q; without parentheses or brackets.
Step 2: Analyze options
queue<int> q; matches correct syntax. queue q<int>; has wrong order. queue<int> q(); looks like a function declaration. queue<int> q[]; uses array syntax incorrectly.
3. What will be the output of the following C++ code?
std::queue<int> q;
q.push(10);
q.push(20);
q.push(30);
int x = q.front();
q.pop();
int y = q.front();
std::cout << x << " " << y << std::endl;
medium
A. 10 20
B. 20 30
C. 30 20
D. 10 30
Solution
Step 1: Trace queue operations
Elements pushed in order: 10, 20, 30. Front is 10 initially.
Step 2: After pop and front calls
After q.pop(), 10 is removed. New front is 20. So x=10, y=20.
Final Answer:
10 20 -> Option A
Quick Check:
First out is 10, next front is 20 [OK]
Hint: front() shows first element; pop() removes it [OK]
Common Mistakes:
Assuming pop() returns value
Mixing front() and back() values
Confusing order of elements
4. Identify the error in the following C++ code snippet using queue:
std::queue<int> q;
q.push(5);
q.pop();
int val = q.front();
std::cout << val << std::endl;
medium
A. Accessing front() on an empty queue causes undefined behavior
B. pop() does not remove element from queue
C. push() is used incorrectly
D. Missing include for queue header
Solution
Step 1: Analyze queue state after operations
After push(5), queue has one element. pop() removes it, queue becomes empty.
Step 2: Check front() call on empty queue
Calling front() on empty queue is undefined and causes runtime error.
Final Answer:
Accessing front() on an empty queue causes undefined behavior -> Option A
Quick Check:
front() on empty queue = error [OK]
Hint: Never call front() if queue might be empty [OK]
Common Mistakes:
Ignoring empty queue before front()
Thinking pop() returns value
Assuming push() syntax is wrong
5. You have a queue with elements 1, 2, 3, 4 (front to back). You perform these operations: q.pop(); q.push(5); q.pop(); q.push(6); What is the final order of elements in the queue from front to back?