From a83e42ede117dfcdcd60db91dcccc7e7d9247f46 Mon Sep 17 00:00:00 2001 From: Seth Jacobson <210383971+sjacobson-pm@users.noreply.github.com> Date: Tue, 2 Dec 2025 10:51:44 -0500 Subject: [PATCH 1/5] Add queue concept to c-sharp docs --- content/c-sharp/concepts/queue/queue.md | 104 ++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 content/c-sharp/concepts/queue/queue.md diff --git a/content/c-sharp/concepts/queue/queue.md b/content/c-sharp/concepts/queue/queue.md new file mode 100644 index 00000000000..f45b7bf6a9c --- /dev/null +++ b/content/c-sharp/concepts/queue/queue.md @@ -0,0 +1,104 @@ +--- +Title: 'Queue' +Description: 'Queue is a type of data structure that allows insertion of elements to the rear and removal of elements from the front.' +Subjects: + - 'Computer Science' + - 'Code Foundations' +Tags: + - 'Queues' + - 'Data Structures' + - 'Data Types' +CatalogContent: + - 'learn-c-sharp' + - 'paths/computer-science' +--- + +`Queue` is a data structure representing a first-in, first-out collection of objects that allows insertion of elements to the rear and removal of elements from the front, with the additional capability of inspecting the front-most element without removing it. In `C#`, it is implemented by the `Queue` class from `.NET`. + +## Queue Operations + +There are three main methods that `Queue` exposes: Enqueue, Dequeue, and Peek. + +- `Enqueue` adds an element to the end of the `Queue` instance. +- `Dequeue` removes an element from the front of the `Queue` instance. +- `Peek` returns the front-most element in the `Queue` instance without removing it. + +## Example + +The below example shows how to instantiate and use a `Queue`. + +```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 + +Use this example to experiment with using a `Queue`. Enjoy coding! + +```codebyte/csharp +using System; +using System.Collections.Generic; + +public class Example +{ + public static void Main() + { + Queue queue = new Queue(); + + // Add elements to the rear + queue.Enqueue(1); + queue.Enqueue(2); + + // Peek at the front element + int peek = queue.Peek(); // returns front element without removing it + + // Remove elements from the front + int front = queue.Dequeue(); // removes 1 from the queue + int second = queue.Dequeue(); // removes 2 from the queue + } +} +``` + +## Notes + +1. The `Peek()` and `Dequeue()` methods will throw an `InvalidOperationException` if the queue is empty. +2. `Enqueue()`, `Dequeue()`, and `Peek()` are O(1) amortized. +3. `Queue` is not thread-safe. Consider using `ConcurrentQueue` for concurrent access. From 97b2660d47542be25cb4245b66b8b5dc72080051 Mon Sep 17 00:00:00 2001 From: Seth Jacobson <210383971+sjacobson-pm@users.noreply.github.com> Date: Tue, 2 Dec 2025 11:19:22 -0500 Subject: [PATCH 2/5] Revise queue.md to match concept template --- content/c-sharp/concepts/queue/queue.md | 37 +++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/content/c-sharp/concepts/queue/queue.md b/content/c-sharp/concepts/queue/queue.md index f45b7bf6a9c..66f9ea5cdd8 100644 --- a/content/c-sharp/concepts/queue/queue.md +++ b/content/c-sharp/concepts/queue/queue.md @@ -1,28 +1,36 @@ --- Title: 'Queue' -Description: 'Queue is a type of data structure that allows insertion of elements to the rear and removal of elements from the front.' +Description: 'Queue is a first-in, first-out (FIFO) data structure that allows insertion of elements to the read and removal of elements from the front.' Subjects: - - 'Computer Science' - 'Code Foundations' + - 'Computer Science' Tags: - - 'Queues' + - 'Collections' - 'Data Structures' - - 'Data Types' -CatalogContent: + - 'Queues' +CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first - 'learn-c-sharp' - 'paths/computer-science' --- -`Queue` is a data structure representing a first-in, first-out collection of objects that allows insertion of elements to the rear and removal of elements from the front, with the additional capability of inspecting the front-most element without removing it. In `C#`, it is implemented by the `Queue` class from `.NET`. +**`Queue`** is a data structure 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. ## Queue Operations There are three main methods that `Queue` exposes: Enqueue, Dequeue, and Peek. -- `Enqueue` adds an element to the end of the `Queue` instance. -- `Dequeue` removes an element from the front of the `Queue` instance. +- `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. +## Notes + +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 element 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(int)` method. + ## Example The below example shows how to instantiate and use a `Queue`. @@ -71,12 +79,11 @@ Dequeue: five ## Codebyte Example -Use this example to experiment with using a `Queue`. Enjoy coding! +Use this example to experiment with using a `Queue`. `T`, in this example, is `int`. Enjoy coding! ```codebyte/csharp using System; using System.Collections.Generic; - public class Example { public static void Main() @@ -86,19 +93,13 @@ public class Example // Add elements to the rear queue.Enqueue(1); queue.Enqueue(2); - + // Peek at the front element int peek = queue.Peek(); // returns front element without removing it - + // Remove elements from the front int front = queue.Dequeue(); // removes 1 from the queue int second = queue.Dequeue(); // removes 2 from the queue } } ``` - -## Notes - -1. The `Peek()` and `Dequeue()` methods will throw an `InvalidOperationException` if the queue is empty. -2. `Enqueue()`, `Dequeue()`, and `Peek()` are O(1) amortized. -3. `Queue` is not thread-safe. Consider using `ConcurrentQueue` for concurrent access. From 41f3f04179c92d55f9d17972515d3f4eba2af5e0 Mon Sep 17 00:00:00 2001 From: Seth Jacobson <210383971+sjacobson-pm@users.noreply.github.com> Date: Tue, 2 Dec 2025 11:24:54 -0500 Subject: [PATCH 3/5] Remove comment --- content/c-sharp/concepts/queue/queue.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/c-sharp/concepts/queue/queue.md b/content/c-sharp/concepts/queue/queue.md index 66f9ea5cdd8..362c534d89a 100644 --- a/content/c-sharp/concepts/queue/queue.md +++ b/content/c-sharp/concepts/queue/queue.md @@ -8,7 +8,7 @@ Tags: - 'Collections' - 'Data Structures' - 'Queues' -CatalogContent: # Please use course/path landing page slugs, rather than linking to individual content items. If listing multiple items, please put the most relevant one first +CatalogContent: - 'learn-c-sharp' - 'paths/computer-science' --- From a5b6c90ded9caa99151ef7419d44cac52f41a4d9 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 6 Dec 2025 11:24:51 +0530 Subject: [PATCH 4/5] Fix description and improve queue documentation --- content/c-sharp/concepts/queue/queue.md | 33 +++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/content/c-sharp/concepts/queue/queue.md b/content/c-sharp/concepts/queue/queue.md index 362c534d89a..46507c87cdc 100644 --- a/content/c-sharp/concepts/queue/queue.md +++ b/content/c-sharp/concepts/queue/queue.md @@ -1,6 +1,6 @@ --- Title: 'Queue' -Description: 'Queue is a first-in, first-out (FIFO) data structure that allows insertion of elements to the read and removal of elements from the front.' +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' @@ -13,27 +13,27 @@ CatalogContent: - 'paths/computer-science' --- -**`Queue`** is a data structure 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. +**`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. ## Queue Operations -There are three main methods that `Queue` exposes: Enqueue, Dequeue, and Peek. +`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. +- `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. -## Notes +## 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 element 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(int)` method. +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 -The below example shows how to instantiate and use a `Queue`. +In this example a queue is created, populated with strings, and processed using `Peek()` and `Dequeue()`: ```cs using System; @@ -79,27 +79,28 @@ Dequeue: five ## Codebyte Example -Use this example to experiment with using a `Queue`. `T`, in this example, is `int`. Enjoy coding! +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(); - // Add elements to the rear queue.Enqueue(1); queue.Enqueue(2); - - // Peek at the front element - int peek = queue.Peek(); // returns front element without removing it - - // Remove elements from the front - int front = queue.Dequeue(); // removes 1 from the queue - int second = queue.Dequeue(); // removes 2 from the queue + + Console.WriteLine("Peek: " + queue.Peek()); + + int first = queue.Dequeue(); + int second = queue.Dequeue(); + + Console.WriteLine("Dequeue 1: " + first); + Console.WriteLine("Dequeue 2: " + second); } } ``` From 620893b342d178439599f1130b5c78c4e0e2a06f Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Sat, 6 Dec 2025 11:27:40 +0530 Subject: [PATCH 5/5] added syntax secton --- content/c-sharp/concepts/queue/queue.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/content/c-sharp/concepts/queue/queue.md b/content/c-sharp/concepts/queue/queue.md index 46507c87cdc..8a9c5aeaadb 100644 --- a/content/c-sharp/concepts/queue/queue.md +++ b/content/c-sharp/concepts/queue/queue.md @@ -15,6 +15,22 @@ CatalogContent: **`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: