Queue
In a queue, what is requested first is handled first. In C# the Queue
type is a FIFO collection. It processes elements in a first-in, first-out order.
Queue
handles the elements that it received longest ago first. To further understand Queue
, it is helpful to examine some uses of this generic class
.
Here we demonstrate the use of the Enqueue method on the Queue
. Enqueue adds to the end of a Queue
. We often call it multiple times.
Queue
. These could correspond to requests.Queue
. We added 4 ints, so there are 4 elements.Queue
, because they will be dealt with last.using System; using System.Collections.Generic; // Step 1: create a new Queue of integers. var elements = new Queue<int>(); elements.Enqueue(5); elements.Enqueue(10); elements.Enqueue(15); elements.Enqueue(20); // Step 2: write count of elements. Console.WriteLine("QUEUE COUNT: {0}", elements.Count);QUEUE COUNT: 4
Next we loop through the elements in the Queue
. Frequently you will need to loop through the elements. We can do this is the standard way.
foreach
-loop calls the Queue
's enumerator, and you can use the standard foreach
syntax.foreach
statement is invoking the Queue
's enumerator. The foreach
syntax hides this.using System; using System.Collections.Generic; // Add integers to queue. Queue<int> collection = new Queue<int>(); collection.Enqueue(5); collection.Enqueue(6); // Loop through queue. foreach (int value in collection) { Console.WriteLine(value); }5 6
This code could be adapted into a help request processing system. You don't want your company's visitors to end up waiting forever. The Queue
ensures this won't happen.
RequestTypes
are added to the Queue
. New requests are the last to be processed.using System; using System.Collections.Generic; class Program { enum RequestType { MouseProblem, TextProblem, ScreenProblem, ModemProblem }; static void Main() { // Initialize help request Queue. Queue<RequestType> help = new Queue<RequestType>(); // Website records a Text Problem help request. help.Enqueue(RequestType.TextProblem); // Website records a Screen Problem help request. help.Enqueue(RequestType.ScreenProblem); // You become available to take a new request. // ... You can't help with Mouse problems. if (help.Count > 0 && help.Peek() != RequestType.MouseProblem) { // You solve the request. // ... It was a TextProblem. help.Dequeue(); } // Record a Modem Problem help request. help.Enqueue(RequestType.ModemProblem); } }
We can copy Queue
elements. We might have a Queue
collection but need to loop over the elements in the reverse order. We call CopyTo
, and then loop over the array in reverse.
CopyTo
with an int
array. We allocate the number of elements to the int
array with the same integer as the Queue
's Count
.for
-loop syntax, which gives more power when iterating over the arrays.Console
in front to back order, and then it is written in the opposite direction.using System; using System.Collections.Generic; // New Queue of integers. Queue<int> queue = new Queue<int>(); queue.Enqueue(5); queue.Enqueue(10); queue.Enqueue(15); queue.Enqueue(20); // Create new array with Length equal to Queue's element count. int[] array = new int[queue.Count]; // Copy the Queue to the int array. queue.CopyTo(array, 0); // Loop through and display int[] in order. Console.WriteLine("Array:"); for (int i = 0; i < array.Length; i++) { Console.WriteLine(array[i]); } // Loop through int array in reverse order. Console.WriteLine("Array reverse order:"); for (int i = array.Length - 1; i >= 0; i--) { Console.WriteLine(array[i]); }Array: 5 10 15 20 Array reverse order: 20 15 10 5
Contains
The Contains()
method works in the same way as the List
Contains
method. It searches iteratively through the queue. The Queue
does not use hashing so the search takes linear time.
using System; using System.Collections.Generic; var elements = new Queue<string>(); elements.Enqueue("bird"); // Search Queue with Contains. if (elements.Contains("bird")) { Console.WriteLine("CONTAINS: BIRD"); }CONTAINS: BIRD
The performance of Queue
, and the List.Insert
method, should be tested. Using Insert
on a List
in the wrong way can hurt performance.
In .NET the Queue
generic class
provides methods to add (enqueue) and remove elements. With Queue
we could process requests. This collection is useful for certain situations.