📖 Scenario: You are managing a line of customers waiting to be served at a coffee shop. You want to keep track of the order in which customers arrive and serve them one by one.
🎯 Goal: Build a simple queue using an array to add customers to the line and remove them when served, showing the queue state after each operation.
📋 What You'll Learn
Create an array to hold the queue elements
Use variables to track the front and rear positions in the queue
Implement enqueue operation to add elements to the queue
Implement dequeue operation to remove elements from the queue
Print the queue elements in order after operations
💡 Why This Matters
🌍 Real World
Queues are used in real life to manage lines, like customers waiting at a store or tasks waiting to be processed.
💼 Career
Understanding queues helps in software development for managing tasks, requests, and resources efficiently.
Progress0 / 4 steps
1
Create the queue array and initialize front and rear
Create an integer array called queue with size 5. Create two integer variables called front and rear and set both to -1.
DSA C#
Hint
Use int[] queue = new int[5]; to create the array. Set front and rear to -1 to show the queue is empty.
2
Add a customer to the queue (enqueue)
Write code to add a customer with ID 101 to the queue. Increase rear by 1 and assign queue[rear] to 101. If front is -1, set it to 0.
DSA C#
Hint
Increase rear first, then add the customer ID to queue[rear]. Set front to 0 if queue was empty.
3
Remove a customer from the queue (dequeue)
Write code to remove the customer at the front of the queue. Store the removed customer ID from queue[front] into a variable called removedCustomer. Then increase front by 1. If front becomes greater than rear, reset both front and rear to -1.
DSA C#
Hint
Store the removed customer ID before moving front. Reset front and rear if queue becomes empty.
4
Print the current queue elements
Write a for loop to print all customer IDs in the queue from front to rear separated by spaces. If the queue is empty (front is -1), print "Queue is empty".
DSA C#
Hint
Check if front is -1 to print empty message. Otherwise, loop from front to rear and print each element.
Practice
(1/5)
1. What is the main characteristic of a queue implemented using an array?
easy
A. It removes elements from the end only.
B. It allows random access to elements.
C. It stores elements in sorted order automatically.
D. It follows First-In-First-Out (FIFO) order.
Solution
Step 1: Understand queue behavior
A queue adds elements at the rear and removes from the front, following FIFO order.
Step 2: Compare options with queue properties
Only It follows First-In-First-Out (FIFO) order. correctly describes FIFO behavior; others describe different data structures or incorrect behavior.
Final Answer:
It follows First-In-First-Out (FIFO) order. -> Option D
Quick Check:
Queue = FIFO order [OK]
Hint: Remember queue = first in, first out [OK]
Common Mistakes:
Confusing queue with stack (LIFO)
Thinking queue allows random access
Assuming queue sorts elements automatically
2. Which of the following is the correct way to declare a queue array of size 5 in C#?
easy
A. int queue = new int[5];
B. int queue[] = new int(5);
C. int[] queue = new int[5];
D. int[] queue = int[5];
Solution
Step 1: Recall C# array declaration syntax
In C#, arrays are declared as type[] name = new type[size];
Step 2: Check each option
int[] queue = new int[5]; matches correct syntax; others have syntax errors or wrong usage.
Final Answer:
int[] queue = new int[5]; -> Option C
Quick Check:
C# array declaration = int[] name = new int[size] [OK]
Hint: Use type[] name = new type[size] for arrays [OK]
Common Mistakes:
Missing [] after type
Using parentheses instead of brackets
Assigning array without new keyword
3. Consider this C# code for a queue using an array of size 3:
int[] queue = new int[3];
int front = -1, rear = -1;
// Enqueue 10
rear++;
queue[rear] = 10;
if(front == -1) front = 0;
// Enqueue 20
rear++;
queue[rear] = 20;
// Dequeue
int val = queue[front];
front++;
// Enqueue 30
rear++;
queue[rear] = 30;
// Current queue elements from front to rear?