Collections, Queue in C#

Queue in C#

Often you have seen people standing in a line or Queue to purchase things in grocery stores. Queue works by first come first served basis which means that the person standing at the front of the Queue will be served first and then the person standing behind him will be served and this continues until the last member of the Queue is served. In Data Structures this concept called Queue in C# is also implemented in the form of Data Structure and as C# has a built-in library with the name of Collection which consists of several data structures among those Data Structures Queue is also a Data Structure which is present in C#. Queue works like a Stack, but the only difference is in Dequeening.

Stack Vs Queue

The stack is LIFO (Last In First Out) which means that in Stack the data item which is inserted in the last will be the 1st to be removed from the stack whereas Queue is different in this regard. The queue is FIFO(First In First Out). In Queue the data item which is inserted very early will be the first to be removed and the data item which is inserted at the last will be the last one to be removed from the list. The stack is considered vertical if Stack is considered horizontal then it becomes a Queue.

Example:

To explain this let us consider the following example. In this example, we have some known data sets let’s see that how we can insert those data sets in our Queue.

  1. Open Visual Studio, Create a new project.
  2. Add a new class for main.
  3. Include collection.generic.

 

  1. After that inside the brackets of Student Class, I will have to define the get & set function in the following manner. First I have to define the functions in which I want to set the get & set functions in the following way:
  2. As I have a Student class so, my functions will be.
  3. public int ID { get; set; }
  4. public string Name { get; set; }
  5. public int Fee { get; set; }

Now to explain my other functions I will select on function for demonstration which is:

Now public means that my function is visible to all the classes related to my program and the data which is set in this function can be retrieved by all the other classes and the classes don’t need any permission to retrieve the data set in this function. int means that the return type of the function is the integer type. It means that my function will return an Integer value. ID is the name of my function. { get; set; } shows that my function is setting data of ID by set function and getting data of ID by get function. All the other function will work in the same manner.

Now as for my program, the data sets are pre-defined. So, I will define the data sets in Main() of my program in the following manner.

In the {} brackets of

function first I will make the instance of my Student class in the following manner:

 

 

Now Student1 is the instance of Student class and = new Student() will set the Student data via Student1 instance in Student class. Now ID = 123, means that 123 is set as data in ID and so, is the case with variables Name & Fee.

You can set multiple instances of Student class in the same manner as

 

Declaring and Initializing a Queue in C#

Now in your Student class declare a queue data structure of type Student.

Queue<Student> means that Student class will be processed as a Queue class, and all the features of Queue will be assigned to this class. StudentQueue is the instance for class Student of queue type. new Queue<Student>(); will allow data sets to be saved in Student class in the form of a  Queue.

Enqueue() data in Queue

Push() is a built-in function in C# which can be used once the library of Collection is included in your program. The .Enqueue(Student1); will push the data set of Student1 instance in the StudentQueue instance of  Student class. Similarly, you can push multiple data sets in the form of a queue in Student class which are as follows:

 

Now you can access and display the data sets of your Student class in the following manner.

 

  Displaying Data of a Queue       

The statement  Console.WriteLine(“After Adding item to Queue “); will simply display the sentence enclosed in quotation marks ” “.

foreach () loop will perform the same operations on StudentQueue (the queue instance of the class (Note: That Student is a general instance of class Student whereas StudentQueue is the queue instance of Student class )) enclosed in {}.

Console.WriteLine will print the data set of one instance of Student class suppose Student1 data set with the foreach loop. The starting and ending limit for foreach loop depends on upon the number of data sets defined by you or the number of instances defined by you. As you have made three instances of your Student class as Student1, Student2, Student3. So, the foreach loop will run three times.

 

Count Data in a Queue

Count() is a built-in function in C# which counts the number of data sets or data elements in your program. StudentQueue.Count() means that Count() will count the number of StudentQueue instances of your Student class.

 

Dequeue data from a Queue

Dequeue() is a built-in function in C# which is included in the Collection library in C#. Now Dequeue() will remove one StudentQueue instance of your Student class.

 

Those above-mentioned statements after foreach will show the contents of the data sets when one data set is removed by Dequeue() function.

When again this statement is executed StudentQueue.Dequeue(); then one more data set will be removed from your queue will be minimized by one data set more.

After this when you type this statement Console.WriteLine(“Total item in Queue = ” + StudentQueue.Count()); then the Count() function will count the total number of data sets after the removal of one more data set.

Again when you type those set of statements

Then again the contents of the remaining data sets will be shown to you.

 

Another Scenario:

Now suppose that you don’t have a pre-defined number of data sets, and you don’t know about the contents of those data sets. In this scenario, you will code in the following manner.

First, you have to create a Student class defining your data sets in the following manner:

First Add a Student class by the method mentioned above with your program. Then in this Student class include the library of Collection in the following way:

Using System.Collection.Generic;

Now after that define your class in the following way:

Now in your Main() do the following things:

Download coded example here

Alternate link

Leave a Reply

Your email address will not be published. Required fields are marked *