08 Aug C# Queue
Queue Collections come under System.Collection.Generic namespace. It stores in the FIFO form i.e. First In First Out, unlike Stack which stores in the LIFO form. Enqueue is when data is inserted in the queue, whereas Dequeue is when it is removed.
Create a Queue and Enqueue
Add elements in the Queue with the Enqueue() method in C#. Enqueue means inserting elements to the end of the queue. To create a Queue in C#, with string elements, use <string>. Set the type in angular braces as shown below:
1 2 3 |
Queue<string> queue = new Queue<string>(); |
Let us see an example and create a queue with some string elements:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a queue Queue<string> queue = new Queue<string>(); // Insert 5 elements to the Queue queue.Enqueue("Amphibians"); queue.Enqueue("Reptiles"); queue.Enqueue("Mammals"); queue.Enqueue("Fish"); queue.Enqueue("Birds"); // Display the elements Console.WriteLine("Display the queue elements...\n"); foreach (string s in queue) { Console.WriteLine(s); } } } } |
Output
1 2 3 4 5 6 7 8 9 |
Display the queue elements... Amphibians Reptiles Mammals Fish Birds |
Dequeue
To return the first element (beginning of the queue) from the queue and remove it, use the Dequeue() method in C#. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a queue Queue<string> queue = new Queue<string>(); // Insert 5 elements to the Queue queue.Enqueue("Amphibians"); queue.Enqueue("Reptiles"); queue.Enqueue("Mammals"); queue.Enqueue("Fish"); queue.Enqueue("Birds"); // Display the elements Console.WriteLine("Display the queue elements..."); foreach (string s in queue) { Console.WriteLine(s); } // Removes the first element from the Queue queue.Dequeue(); // Display the updated elements Console.WriteLine("\nDisplay the updated queue elements..."); foreach (string s in queue) { Console.WriteLine(s); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Display the queue elements... Amphibians Reptiles Mammals Fish Birds Display the updated queue elements... Reptiles Mammals Fish Birds |
Count the number of elements
The Count property is used to count the number of elements in the Queue. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a queue Queue<string> queue = new Queue<string>(); // Insert 5 elements to the Queue queue.Enqueue("Amphibians"); queue.Enqueue("Reptiles"); queue.Enqueue("Mammals"); queue.Enqueue("Fish"); queue.Enqueue("Birds"); // Display the elements Console.WriteLine("Display the queue elements..."); foreach (string s in queue) { Console.WriteLine(s); } // Display the count of elements Console.WriteLine("\nNumber of elements = "+queue.Count); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
Display the queue elements... Amphibians Reptiles Mammals Fish Birds Number of elements = 5 |
Check for an element in the queue
The Contains() method is used to check whether an element exists in the queue or not. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a queue Queue<string> queue = new Queue<string>(); // Insert 5 elements to the Queue queue.Enqueue("Amphibians"); queue.Enqueue("Reptiles"); queue.Enqueue("Mammals"); queue.Enqueue("Fish"); queue.Enqueue("Birds"); // Display the elements Console.WriteLine("Display the queue elements..."); foreach (string s in queue) { Console.WriteLine(s); } // Check for an element in the Queue Console.WriteLine("\nAre Mammals in the Queue? = "+queue.Contains("Mammals")); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
Display the queue elements... Amphibians Reptiles Mammals Fish Birds Are Mammals in the Queue? = True |
Peek an element
To return the first element from the top of the queue, without removing it, use the Peek() method. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
using System; using System.Collections.Generic; namespace Studyopedia { class Program { static void Main(string[] args) { // Create a queue Queue<string> queue = new Queue<string>(); // Insert 5 elements to the Queue queue.Enqueue("Amphibians"); queue.Enqueue("Reptiles"); queue.Enqueue("Mammals"); queue.Enqueue("Fish"); queue.Enqueue("Birds"); // Display the elements Console.WriteLine("Display the queue elements..."); foreach (string s in queue) { Console.WriteLine(s); } // Get the first element from the queue Console.WriteLine("\n1st element = "+queue.Peek()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
Display the queue elements... Amphibians Reptiles Mammals Fish Birds 1st element = Amphibians |
Read More
No Comments