C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First-In-First-Out: The queue data structure implements this algorithm. Queue is a generic type with one type parameter.
Enqueue: This method adds a value (of the Queue's specified element type) to the end of the Queue.
C# program that uses Enqueue
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// Create a new Queue of integers.
var elements = new Queue<int>();
// Add 5 to the end.
elements.Enqueue(5);
// Add 10 to the queue, so 10 is now at the start.
elements.Enqueue(10);
// Continue adding to the start.
elements.Enqueue(15);
elements.Enqueue(20);
// Write count of elements.
Console.WriteLine("QUEUE COUNT: {0}", elements.Count);
}
}
Output
QUEUE COUNT: 4
Tip: The foreach-loop calls the Queue's enumerator, and you can use the standard foreach syntax.
Info: Behind the scenes, the foreach statement is invoking the Queue's enumerator. The foreach syntax hides this.
ForeachC# program that loops with Queue
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 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);
}
}
}
Output
5
6
Simulation: This example simulates a system where new help RequestTypes are added to the Queue. New requests are the last to be processed.
Tip: The oldest requests, those that have been waiting the longest, are the closest to being acted on. No one is left hanging infinitely.
C# program that uses Queue for request system
using System;
using System.Collections.Generic;
class Program
{
enum RequestType
{
MouseProblem,
TextProblem,
ScreenProblem,
ModemProblem
};
static void Main()
{
// Initialize help requeust 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);
}
}
CopyTo: We use CopyTo with an int array. We allocate the number of elements to the int[] with the same integer as the Queue's Count.
For: We use the for-loop syntax, which gives more power when iterating over the arrays.
Int ArrayResults: The array is written to the Console in front to back order, and then it is written in the opposite direction.
ConsoleC# program that copies Queue
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 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]);
}
}
}
Output
Array:
5
10
15
20
Array reverse order:
20
15
10
5
C# program that uses Contains on Queue
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var elements = new Queue<string>();
elements.Enqueue("bird");
// Search Queue with Contains.
if (elements.Contains("bird"))
{
Console.WriteLine("CONTAINS: BIRD");
}
}
}
Output
CONTAINS: BIRD
Clear: Use Clear to remove all elements from the Queue. This can be used instead of assigning the reference to a new Queue.
Dequeue: Removes the object at the beginning of your Queue. The algorithmic complexity of this is O(1). It doesn't loop over elements.
Peek: Microsoft: "Returns the object at the beginning of the Queue(T) without removing it." This means you only look at the object.
ToArray: Converts your Queue(T) to an array. This is similar to CopyTo, but it provides the new array reference.
TrimExcess: This is supposed to minimize the Queue(T) collection's memory usage. It avoids doing anything if the Queue is > 90% full.
Count: Returns the number of elements. It is in an O(1) operation, meaning it requires constant time and doesn't enumerate the elements.
Quote: FIFO queues are frequently used within computer systems to hold tasks that are yet to be accomplished when we want to provide services on a first-come, first-served basis (Algorithms in C++ Third Edition).