A Queue is a special type of collection that stores the elements in a First In First Out style, it is exactly the opposite of the Stack collection. It contains the elements in the order they were added.
C# has the generic Queue and non-generic Queue collection.
Queue Properties
The following are some of the common properties of a queue.
Property | Description |
---|---|
Count | It will return the total number of elements in the queue |
IsSynchronized | It is used to get a value to indicate that access to the queue is synchronized (thread-safe) or not. |
Queue Methods
The following are some of the common queue methods to perform operations like add, delete, etc., on elements of a queue.
Method | Description |
---|---|
Enqueue | It is used to add elements at the end of the queue. |
Dequeue | It will remove and returns an item from the starting of a queue. |
Clear | It will remove all the elements from the queue. |
Clone | It will create a shallow copy of the queue. |
Contains | It is used to determine whether an element exists in a queue or not. |
Peek | It is used to get the first element from the queue. |
TrimToSize | It is used to set the capacity of a queue to an actual number of elements in the queue. |
Example
using System; using System.Collections; public class Program { public static void Main() { Queue myqueue = new Queue(); myqueue.Enqueue("Lionel"); myqueue.Enqueue("Messi"); myqueue.Enqueue(34); myqueue.Enqueue(10); myqueue.Enqueue("Barcelona"); Console.WriteLine("******Queue Example******"); Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); Console.WriteLine("******Queue Elements******"); // Access Queue Elements foreach (var item in myqueue) { Console.WriteLine(item); } Console.ReadLine(); } }
You will see the following when run
******Queue Example****** Number of Elements in Queue: 5 ******Queue Elements****** Lionel Messi 34 10 Barcelona
You can create an object of the Queue by specifying a type parameter for the type of elements it can store.
The following example creates and adds elements in the Queue using the Enqueue() method. A Queue collection allows null and duplicate values.
using System; using System.Collections.Generic; public class Program { public static void Main() { Queue<int> lottery = new Queue<int>(); lottery.Enqueue(11); lottery.Enqueue(24); lottery.Enqueue(43); lottery.Enqueue(34); lottery.Enqueue(4); lottery.Enqueue(29); foreach(var id in lottery) Console.Write(id + " "); Console.ReadLine(); } }
You will see the following
11 24 43 34 4 29
Dequeue() Method
the queue Dequeue() method will always remove and return the first element of the queue.
using System; using System.Collections; public class Program { public static void Main() { Queue myqueue = new Queue(); myqueue.Enqueue("Lionel"); myqueue.Enqueue("Messi"); myqueue.Enqueue(34); myqueue.Enqueue(10); myqueue.Enqueue("Barcelona"); Console.WriteLine("******Queue Example******"); Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); while (myqueue.Count > 0) { Console.WriteLine(myqueue.Dequeue()); } Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); Console.ReadLine(); } }
Run this and you will see this
******Queue Example****** Number of Elements in Queue: 5 Lionel Messi 34 10 Barcelona Number of Elements in Queue: 0
Peek() Method
The queue Peek() method will always return a first element of the queue
using System; using System.Collections; public class Program { public static void Main() { Queue myqueue = new Queue(); myqueue.Enqueue("Lionel"); myqueue.Enqueue("Messi"); myqueue.Enqueue(34); myqueue.Enqueue(10); myqueue.Enqueue("Barcelona"); Console.WriteLine("******Queue Example******"); Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); Console.WriteLine(myqueue.Peek()); Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); Console.ReadLine(); } }
Run this and you will see this
******Queue Example****** Number of Elements in Queue: 5 Lionel Number of Elements in Queue: 5
Contains() Method
The queue Contains() method, you can check whether an element exists in a queue or not. In case if the element is found in the queue, then it will return true otherwise false.
using System; using System.Collections; public class Program { public static void Main() { Queue myqueue = new Queue(); myqueue.Enqueue("Lionel"); myqueue.Enqueue("Messi"); myqueue.Enqueue(34); myqueue.Enqueue(10); myqueue.Enqueue("Barcelona"); Console.WriteLine("******Queue Example******"); Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); Console.WriteLine("Contains Element 4: {0}", myqueue.Contains(4)); Console.WriteLine("Contains Element 34: {0}", myqueue.Contains(34)); Console.WriteLine("Contains Key 'Lionel': {0}", myqueue.Contains("Lionel")); Console.WriteLine("Contains Key 'Billy': {0}", myqueue.Contains("Billy")); Console.WriteLine("Number of Elements in Queue: {0}", myqueue.Count); Console.ReadLine(); } }
You will see the following
******Queue Example****** Number of Elements in Queue: 5 Contains Element 4: False Contains Element 34: True Contains Key 'Lionel': True Contains Key 'Billy': False Number of Elements in Queue: 5
Notes
Queue is a First In First Out collection.
You need to add a reference to the System.Collection.Generic namespace.
Queue can contain elements of the specified type. It provides compile-time type checking and doesn’t perform boxing-unboxing because it is generic.
Elements can be added using the Enqueue() method.
Elements can be retrieved using the Dequeue() and the Peek() methods.