The Queue is a bit particular as it’s based on the First In, First out (or first come first served) principle. This is exactly like when you’re in line to get your favorite coffee: the first customer that arrived is served first and so on. Here is an image to better illustrate the situation.
It’s the same thing in a Queue, the first element that you insert will be the first one to be removed.
The Queue is a Generic collection, that means it can take any type of information of a type specified at its declaration and has been specifically created to wrap the First come, first served principle. The collection is really convenient to use for anything ranging from email delivery queue to thread pool for example. There are several situations in which you will want to use a Queue instead of a List, again, the important part is that you know this type of collection exists so you can figure out when to use it!
Here is how to declare a Queue that can contain elements of type String. In this example I am going to simulate a car production Queue.
Visual Basic
' Create new Queue of string. Dim CarProductionQueue = New Queue(Of String)
C#
// Create new Queue of string. Queue<string> carProductionQueue = new Queue<string>();
Enqueue(T Element)
To add items to a Queue, you need to use the Enqueue(T Element) method instead of add(T Element). That method name makes a bit more sense as well! Here is an example.
Visual Basic
' Enqueue new elements. CarProductionQueue.Enqueue("Toyota GT86") CarProductionQueue.Enqueue("Scion FRS") CarProductionQueue.Enqueue("Subaru BRZ")
C#
// Enqueue new elements. carProductionQueue.Enqueue("Toyota GT86"); carProductionQueue.Enqueue("Scion FRS"); carProductionQueue.Enqueue("Subaru BRZ");
In the code above, I am adding 3 cars in the car production Queue (String because its only the car brand and name). That’s why I am calling the Enqueue(String CarToEnqueue) method three times.
Dequeue()
Instead of Remove(), we’re using Dequeue() to retrieve the next element. It then returns the Element that has been dequeued and you need to retrieve that element if you wish to use it in some way. In the following example, I’m using the element directly without storing it in a variable in between the steps. Remember that once the element is dequeued, it is not contained in the Queue anymore!
Visual Basic
' Dequeue an element from the ProductionQueue. Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue())
C#
// Dequeue each element from the ProductionQueue. Console.WriteLine("Production complete for " + carProductionQueue.Dequeue()); Console.WriteLine("Production complete for " + carProductionQueue.Dequeue()); Console.WriteLine("Production complete for " + carProductionQueue.Dequeue());
I am calling the Dequeue() method three times because there are three elements in the Queue, if you try to call the method when no elements are left in the collection, it’s going to throw an exception and your code will crash. Make sure that there is at least one element in the Queue before you Dequeue!
Count
Here is a way to output all the elements while making sure the Queue is not empty! But you can surely find other ways to do the same thing!
Visual Basic
' Same Dequeue operation but using the Count Property with a loop. While CarProductionQueue.Count > 0 Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) End While
C#
// Same Dequeue operation but using the Count Property with a loop. while (carProductionQueue.Count > 0) Console.WriteLine("Production complete for " + carProductionQueue.Dequeue());
The Count parameter will give you the number of elements in the Queue at the current time and can be useful to avoid a code crash by checking how many elements are left in the collection.
Complete Code
Here is the complete code containing three ways you can dequeue elements from your Queue.
Visual Basic
Module Program Sub Main() ' Create new Queue of string. Dim CarProductionQueue = New Queue(Of String) ' Enqueue new elements in the ProductionQueue. CarProductionQueue.Enqueue("Toyota GT86") CarProductionQueue.Enqueue("Scion FRS") CarProductionQueue.Enqueue("Subaru BRZ") '' Dequeue elements from the ProductionQueue (one by one). 'Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) 'Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) 'Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) ' Dequeue with a While loop. While CarProductionQueue.Count > 0 Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) End While '' Dequeue with a For loop 'Dim NumberOfCarsInQueue = CarProductionQueue.Count 'For Index As Integer = 1 To NumberOfCarsInQueue Step 1 ' Console.WriteLine("Production complete for " & CarProductionQueue.Dequeue()) 'Next Console.ReadKey() End Sub End Module
C#
using System; using System.Collections.Generic; namespace CSharpQueue { class Program { static void Main(string[] args) { // Create new Queue of string. Queue<string> carProductionQueue = new Queue<string>(); // Enqueue new elements. carProductionQueue.Enqueue("Toyota GT86"); carProductionQueue.Enqueue("Scion FRS"); carProductionQueue.Enqueue("Subaru BRZ"); // Dequeue each element from the ProductionQueue. //Console.WriteLine("Production complete for " + carProductionQueue.Dequeue()); //Console.WriteLine("Production complete for " + carProductionQueue.Dequeue()); //Console.WriteLine("Production complete for " + carProductionQueue.Dequeue()); // Same Dequeue operation but using the Count Property with a loop. while (carProductionQueue.Count > 0) Console.WriteLine("Production complete for " + carProductionQueue.Dequeue()); Console.ReadKey(); } } }
There is something important to remember about collections like we’re seeing right now: they are not thread safe! What is a thread? we will dive into that topic later but that’s still something to remember.
Found an error in the code? Please send me a message so I can fix it as soon as possible!
No Comments Yet!
You can be first to comment this post!