Queues - potatoscript/dsa GitHub Wiki
A queue is like a line at your favorite amusement park π’! When you stand in line to ride a roller coaster, the first person in line is the first one to ride the coaster. π°
In computer science, this is called First In, First Out (FIFO) β the first item that enters the queue is the first one to leave. Think of it like waiting your turn in line: the person who comes first gets served first.
Front β [Person 1] β [Person 2] β [Person 3] β Back
If a new person joins the line (letβs call them "Person 4"), they go to the back:
Front β [Person 1] β [Person 2] β [Person 3] β [Person 4] β Back
And when it's time to serve someone (letβs say "Person 1"), they get served first from the front:
Serve Person 1: Front β [Person 2] β [Person 3] β [Person 4] β Back
Queues are used in many real-world situations:
- β Print Jobs: Think about when you send multiple documents to print. The first document you send will be the first to print.
- β Task Scheduling: The operating system handles tasks in a queue, the first task in is the first to be processed.
- β Handling Requests: When servers handle multiple requests from users, the first request to arrive is the first to be processed.
Situation | Queue Comparison |
---|---|
π’ Theme park line | First person in line rides first |
π Fast-food drive-thru | First car in line gets served first |
π§ Email inbox | The first email you receive is the first one you read |
In C#, we can use the Queue<T>
class to easily implement a queue:
Queue<string> customerQueue = new Queue<string>();
customerQueue.Enqueue("Customer 1");
customerQueue.Enqueue("Customer 2");
customerQueue.Enqueue("Customer 3");
string servedCustomer = customerQueue.Dequeue(); // Serves "Customer 1"
Console.WriteLine("Served: " + servedCustomer);
If you just want to see who is at the front without removing them, you can peek:
string frontCustomer = customerQueue.Peek(); // Shows "Customer 2" without removing
Console.WriteLine("Front customer: " + frontCustomer);
Operation | Description | Time Complexity |
---|---|---|
Enqueue |
Add an item to the end of the queue | O(1) |
Dequeue |
Remove the front item from the queue | O(1) |
Peek |
View the front item without removing it | O(1) |
IsEmpty |
Check if the queue is empty | O(1) |
Queue<string> customerQueue = new Queue<string>();
customerQueue.Enqueue("Customer 1");
customerQueue.Enqueue("Customer 2");
customerQueue.Enqueue("Customer 3");
Console.WriteLine("Queue after enqueueing customers:");
foreach (var customer in customerQueue)
{
Console.WriteLine(customer);
}
Output:
Customer 1
Customer 2
Customer 3
string servedCustomer = customerQueue.Dequeue();
Console.WriteLine("Served Customer: " + servedCustomer);
Console.WriteLine("Queue after serving a customer:");
foreach (var customer in customerQueue)
{
Console.WriteLine(customer);
}
Output:
Served Customer: Customer 1
Customer 2
Customer 3
- β Dequeueing from an empty queue: You canβt remove an item from an empty queue! Always check if the queue is empty before dequeuing.
if (customerQueue.Count > 0)
{
customerQueue.Dequeue();
}
else
{
Console.WriteLine("Queue is empty!");
}
In many operating systems, tasks are placed in a queue and processed in the order they arrive. This is how tasks are scheduled to execute on your computer, ensuring fairness.
When several print jobs are sent to a printer, they are placed in a queue. The first print job that arrives is the first to be printed.
Queue<string> taskQueue = new Queue<string>();
// Adding tasks
taskQueue.Enqueue("Task 1");
taskQueue.Enqueue("Task 2");
taskQueue.Enqueue("Task 3");
// Processing tasks
while (taskQueue.Count > 0)
{
string task = taskQueue.Dequeue();
Console.WriteLine("Processing " + task);
}
- Enqueue: O(1) β Adding to the end is quick!
- Dequeue: O(1) β Removing from the front is also quick!
- Peek: O(1) β Viewing the front without changing the queue is fast!
Create a queue of print jobs and remove them one by one, simulating a printer processing the jobs.
Simulate a customer service line where customers are added to the queue and served one by one.
Write a program that simulates a task scheduler. Add a few tasks to the queue and process them one by one.
- Single Queue: A simple, one-line queue.
- Circular Queue: The queue βwraps aroundβ when the end is reached, using the available space more efficiently.
- Priority Queue: Items with higher priority are processed before items with lower priority.
- Double-Ended Queue (Deque): A queue where you can add or remove items from both ends.
β
Queues store data in a First In, First Out (FIFO) order.
β
Add (Enqueue
) to the back, remove (Dequeue
) from the front.
β
Use them for managing tasks, print jobs, customer service lines, and more!
β
Big O for queue operations: O(1) β fast and efficient!