diff --git a/content/c-sharp/concepts/queue/queue.md b/content/c-sharp/concepts/queue/queue.md new file mode 100644 index 00000000000..8a9c5aeaadb --- /dev/null +++ b/content/c-sharp/concepts/queue/queue.md @@ -0,0 +1,122 @@ +--- +Title: 'Queue' +Description: 'Queue is a first-in, first-out (FIFO) data structure that allows insertion of elements to the rear and removal of elements from the front.' +Subjects: + - 'Code Foundations' + - 'Computer Science' +Tags: + - 'Collections' + - 'Data Structures' + - 'Queues' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +**`Queue`** is a [data structure](https://www.codecademy.com/resources/docs/general/data-structures) representing a first-in, first-out (FIFO) collection of elements that allows insertion of new elements to the rear, and removal of elements from the front. + +## Syntax + +The syntax to create a new queue is: + +```pseudo +Queue queueName = new Queue(); +``` + +**Parameters:** + +- `T`: The type of elements the queue will store. + +**Return value:** + +Creating a queue with new `Queue()` returns a new empty `Queue` instance. + +## Queue Operations + +`Queue` provides three primary operations: + +- `Enqueue` adds an element to the end of the `Queue` instance. `null` is an acceptable value for reference type elements. +- `Dequeue` returns and removes an element from the front of the `Queue` instance. +- `Peek` returns the front-most element in the `Queue` instance without removing it. + +## Key Behaviors of a Queue + +1. `Queue` accepts `null` as a valid value for reference types, and allows insertion of duplicate elements. +2. The `Peek()` and `Dequeue()` methods will throw an `InvalidOperationException` if the queue is empty. +3. `Enqueue()`, `Dequeue()`, and `Peek()` are O(1) amortized. +4. `Queue` is not thread-safe. Consider using `ConcurrentQueue` for concurrent access. +5. The `Capacity` property returns the maximum number of elements the `Queue` instance can currently hold. This value is increased automatically as elements are added to the collection, and can be reduced by calling the `TrimExcess()` method. + +## Example + +In this example a queue is created, populated with strings, and processed using `Peek()` and `Dequeue()`: + +```cs +using System; +using System.Collections.Generic; + +class Program +{ + static void Main() + { + Queue queue = new Queue(); + + // Adding elements to the back of the queue + queue.Enqueue("one"); + queue.Enqueue("two"); + queue.Enqueue("three"); + queue.Enqueue("four"); + queue.Enqueue("five"); + + // Peeking the front-most element + string peek = queue.Peek(); + Console.WriteLine($"Peek: {peek}"); + + // Removing elements from the front of the queue + while (queue.Count > 0) + { + string front = queue.Dequeue(); + Console.WriteLine($"Dequeue: {front}"); + } + } +} +``` + +Output: + +```shell +Peek: one +Dequeue: one +Dequeue: two +Dequeue: three +Dequeue: four +Dequeue: five +``` + +## Codebyte Example + +In this example a queue of integers is used to demonstrate peeking at the front element and removing items in FIFO order: + +```codebyte/csharp +using System; +using System.Collections.Generic; + +public class Example +{ + public static void Main() + { + Queue queue = new Queue(); + + queue.Enqueue(1); + queue.Enqueue(2); + + Console.WriteLine("Peek: " + queue.Peek()); + + int first = queue.Dequeue(); + int second = queue.Dequeue(); + + Console.WriteLine("Dequeue 1: " + first); + Console.WriteLine("Dequeue 2: " + second); + } +} +```