📖 Scenario: Imagine you are managing a line of customers waiting to buy tickets at a movie theater. Each new customer joins the end of the line. We will simulate this line using a queue data structure.
🎯 Goal: You will create a queue, add customers to it using the enqueue operation, and then display the queue to see the order of customers waiting.
📋 What You'll Learn
Create a queue of strings called ticketQueue.
Add three customers named exactly "Alice", "Bob", and "Charlie" to the queue using the enqueue operation.
Create a variable called newCustomer and set it to "Diana".
Add the newCustomer to the queue using the enqueue operation.
Print the contents of ticketQueue in order, separated by " -> ", ending with " -> null".
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting for service, print jobs waiting to print, or tasks waiting to be processed.
💼 Career
Understanding queues and the enqueue operation is important for software developers working on scheduling, task management, and real-time systems.
Progress0 / 4 steps
1
Create the initial queue
Create a queue of strings called ticketQueue and add these customers in order using Enqueue: "Alice", "Bob", "Charlie".
DSA C#
Hint
Use Queue<string> to create the queue and Enqueue to add customers.
2
Add a new customer variable
Create a string variable called newCustomer and set it to "Diana".
DSA C#
Hint
Use string newCustomer = "Diana"; to create the variable.
3
Enqueue the new customer
Add the newCustomer to the ticketQueue using the Enqueue method.
DSA C#
Hint
Use ticketQueue.Enqueue(newCustomer); to add the new customer.
4
Print the queue contents
Print the contents of ticketQueue in order, separated by " -> ", and end with " -> null". Use a foreach loop with variable customer to iterate over ticketQueue.
DSA C#
Hint
Use a foreach loop to print each customer followed by " -> ", then print "null" at the end.
Practice
(1/5)
1. What does the Enqueue operation do in a queue data structure?
easy
A. Adds an element to the end of the queue
B. Removes an element from the front of the queue
C. Checks if the queue is empty
D. Returns the size of the queue
Solution
Step 1: Understand queue behavior
A queue works like a line where new people join at the end.
Step 2: Define enqueue operation
Enqueue means adding a new element at the back of the queue.
Final Answer:
Adds an element to the end of the queue -> Option A
Quick Check:
Enqueue = Add at end [OK]
Hint: Enqueue means add at the back of the queue [OK]
Common Mistakes:
Confusing enqueue with dequeue
Thinking enqueue removes elements
Mixing enqueue with checking size
2. Which of the following is the correct syntax to enqueue an integer value 10 into a Queue<int> named q in C#?
easy
A. q.Enqueue(10);
B. q.Add(10);
C. q.Push(10);
D. q.Insert(10);
Solution
Step 1: Recall C# Queue method
The method to add elements in Queue<T> is Enqueue.
Step 2: Match syntax
Calling q.Enqueue(10); adds 10 to the queue correctly.
Final Answer:
q.Enqueue(10); -> Option A
Quick Check:
Use Enqueue() to add in C# Queue [OK]
Hint: Use Enqueue() method to add elements in C# Queue [OK]
Common Mistakes:
Using Add() which is not in Queue
Using Push() which is for stacks
Using Insert() which is not a Queue method
3. What will be the output of the following C# code?
4. Identify the error in the following code snippet that tries to enqueue an element into a queue:
Queue<int> q = new Queue<int>();
q.Enqueue(5);
q.Enqueue(10);
q.Enqueue(15);
q.Enqueue();
medium
A. Cannot enqueue integer values
B. Queue is not initialized
C. Enqueue method does not exist
D. Missing argument in the last Enqueue call
Solution
Step 1: Check method calls
The last call q.Enqueue(); has no argument, but Enqueue requires one.
Step 2: Confirm method signature
Enqueue needs a value to add, so missing argument causes error.
Final Answer:
Missing argument in the last Enqueue call -> Option D
Quick Check:
Enqueue requires one argument [OK]
Hint: Enqueue always needs one argument to add [OK]
Common Mistakes:
Calling Enqueue without argument
Assuming Enqueue can be empty
Confusing initialization errors
5. You have a queue q with elements [1, 2, 3]. You want to enqueue elements 4 and 5, but only if the queue size is less than 5. Which code snippet correctly performs this conditional enqueue operation in C#?
hard
A. if (q.Count == 5) { q.Enqueue(4); q.Enqueue(5); }
B. if (q.Count <= 5) { q.Enqueue(4); q.Enqueue(5); }
C. if (q.Count < 5) { q.Enqueue(4); q.Enqueue(5); }
D. if (q.Count > 5) { q.Enqueue(4); q.Enqueue(5); }
Solution
Step 1: Understand queue size condition
We want to add only if size is less than 5, so condition is q.Count < 5.
Step 2: Apply enqueue calls inside condition
If condition is true, enqueue 4 and 5 to add them safely.
Final Answer:
if (q.Count < 5) { q.Enqueue(4); q.Enqueue(5); } -> Option C
Quick Check:
Use < 5 to check size before enqueue [OK]
Hint: Check queue size with Count before enqueueing [OK]
Common Mistakes:
Using <= instead of < causing overflow
Using > or == which are wrong conditions
Not checking size before enqueue
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.