📖 Scenario: You are managing a line of customers waiting to buy tickets at a counter. The line works on a first-come, first-served basis. When a new customer arrives, they join the end of the line.
🎯 Goal: Build a simple queue using an array and implement the enqueue operation to add new customers to the end of the queue.
📋 What You'll Learn
Create an integer array called queue with size 5 initialized with zeros
Create an integer variable called rear initialized to -1 to track the last position in the queue
Write a function called enqueue that takes an integer value and adds it to the queue if there is space
Print the queue array after adding new elements
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting at a bank or tasks waiting to be processed by a computer.
💼 Career
Understanding queues and enqueue operations is important for software developers working on scheduling, resource management, and real-time systems.
Progress0 / 4 steps
1
Create the queue array and rear variable
Create an integer array called queue of size 5 initialized with zeros and an integer variable called rear set to -1.
DSA C++
Hint
Think of queue as the line of customers and rear as the position of the last customer.
2
Create the enqueue function header
Write the function header for enqueue that takes an integer parameter called value.
DSA C++
Hint
Start the function with void enqueue(int value) { to accept the new customer number.
3
Implement enqueue logic to add value to queue
Inside the enqueue function, check if rear is less than 4. If yes, increment rear by 1 and assign value to queue[rear]. Close the function with a closing brace.
DSA C++
Hint
Remember to increase rear before adding the new value to the queue.
4
Add values and print the queue
Call enqueue three times with values 10, 20, and 30. Then use a for loop to print all elements of queue separated by spaces.
DSA C++
Hint
Use std::cout inside a loop from 0 to 4 to print the queue elements.
Practice
(1/5)
1. What does the enqueue operation do in a queue data structure?
easy
A. Returns the size of the queue
B. Removes an element from the front of the queue
C. Checks if the queue is empty
D. Adds an element to the end of the queue
Solution
Step 1: Understand the queue structure
A queue works like a line where elements are added at the back and removed from the front.
Step 2: Define the enqueue operation
Enqueue means adding a new element to the end of the queue to wait its turn.
Final Answer:
Adds an element to the end of the queue -> Option D
Quick Check:
Enqueue = Add at end [OK]
Hint: Enqueue always adds at the back of the queue [OK]
Common Mistakes:
Confusing enqueue with dequeue (removal)
Thinking enqueue removes elements
Mixing enqueue with checking queue size
2. Which of the following is the correct syntax to enqueue an element 5 into a queue named q in C++ STL?
easy
A. q.push(5);
B. q.add(5);
C. q.enqueue(5);
D. q.insert(5);
Solution
Step 1: Recall C++ STL queue methods
The standard queue in C++ uses push() to add elements at the back.
Step 2: Match the correct method
enqueue is not a method in STL queue; push is used instead.
Final Answer:
q.push(5); -> Option A
Quick Check:
STL queue uses push() to enqueue [OK]
Hint: Use push() to add elements in C++ STL queue [OK]
Common Mistakes:
Using enqueue() which is not in STL queue
Using add() or insert() which are not queue methods
Confusing push() with pop()
3. What will be the output of the following C++ code snippet?