C#
C# Class & Object Tutorial with Examples
C# is based on the C++ programming language. Hence, the C# programming language has in-built...
The Queue is a special case collection which represents a first in first out concept. Imagine a queue of people waiting for the bus. Normally, the first person who enters the queue will be the first person to enter the bus. Similarly, the last person to enter the queue will be the last person to enter into the bus. Elements are added to the queue, one on the top of each other.
The process of adding an element to the queue is the enqueuer operation. To remove an element from a queue, you can use the dequeuer operation. The operation in queues is similar to stack we saw previously.
Let's look at the operations available for the Queue collection in more detail.
The declaration of a Queue is provided below. A Queue is created with the help of the Queue Data type. The "new" keyword is used to create an object of a Queue. The object is then assigned to the variable qt.
Queue qt = new Queue()
The enqueue method is used to add an element onto the queue. The general syntax of the statement is given below.
Queue.enqueue(element)
The dequeue method is used to remove an element from the queue. The dequeue operation will return the first element of the queue. The general syntax of the statement is given below
Queue.dequeue()
This property is used to get the number of items in the queue. Below is the general syntax of this statement.
Queue.Count
This method is used to see if an element is present in the Queue. Below is the general syntax of this statement. The statement will return true if the element exists, else it will return the value false.
Queue.Contains(element)
Now, let's see this working at a code level. All of the below-mentioned code will be written to our Console application.
The code will be written to our Program.cs file. In the below program, we will write the code to see how we can use the above-mentioned methods.
In this example, we will see how a queue gets created. Next, we will see how to display the elements of the queue, and use the Count and Contain methods.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Queue qt = new Queue();
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);
foreach (Object obj in qt)
{
Console.WriteLine(obj);
}
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("The number of elements in the Queue " + qt.Count);
Console.WriteLine("Does the Queue contain " + qt.Contains(3));
Console.ReadKey();
}
}
} If the above code is entered properly and the program is run the following output will be displayed.
From the output, we can clearly see that the elements of the Queue are displayed. Note that, unlike "stack" in "queue" the first element pushed on to the queue is displayed first. The count of queue elements is also shown in the output. Also, the value of True is displayed to say that the value of 3 is defined on the queue.
Now let's look at the remove functionality. We will see the code required to remove the last element from the queue.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
Queue qt = new Queue();
qt.Enqueue(1);
qt.Enqueue(2);
qt.Enqueue(3);
qt.Dequeue();
foreach (Object obj in qt)
{
Console.WriteLine(obj);
}
Console.ReadKey();
}
}
} If the above code is entered properly and the program is run the following output will be displayed.
From the output, we can see that the first element which was added to the queue, which was the element 1, was removed from the queue.
Summary
C# is based on the C++ programming language. Hence, the C# programming language has in-built...
What is .Net Framework? .Net Framework is a software development platform developed by Microsoft for...
In our previous tutorial, we have learned about how we can use arrays in C#. Let's have a quick...
The concept of Serialization and deserialization is used whenever data pertaining to objects have...
In this tutorial, you will learn- Access Modifiers Constructor Access Modifiers Access Modifiers or...
C# is a general purpose programming language which encompasses various disciplines like...