C # queue introduction and detailed usage guide

Time:2021-9-21

queueA first in first out (FIFO) collection used to represent objects. Used when you need first in first out access to a project. This is a non generic typeGather,staySystem.CollectionsNamespace. According to the needs of the program, it is used to create growingDynamic set。 In the queue, you can store elements of the same type and different types. Typically, a queue is useful when you access this information in the same way as it is stored in a collection. It is a temporary storage for storing data.
C # queue introduction and detailed usage guide

The following figure illustratesQueueHierarchy of classes:
C # queue introduction and detailed usage guide

Important:

  • The queue class implements IEnumerable, icollection and clonable interfaces.
  • When you add an item to the list, it is called joining the team
  • When you delete an item, it is called out of the queue
  • The queue accepts null as a valid value for a reference type.
  • When an element is added to the queue, the capacity is automatically increased as needed by reallocating the internal array.
  • In the queue, you are allowed to store duplicate elements.
  • The capacity of a queue is the number of elements that the queue can hold.

How do I create a queue?

The queue class has four constructors. The methods used to create the queue are as follows:

  • Queue(): this constructor is used to create an instance of the queue class, which is empty, has a default initial capacity, and uses the default growth factor.
  • Queue(ICollection): this constructor is used to create an instance of the queue class that contains elements copied from the specified collection, has the same initial capacity as the number of elements copied, and uses the default growth factor.
  • Queue(Int32): this constructor is used to create an instance of the queue class, which is empty and has the specified initial capacity, and uses the default growth factor.
  • Queue(Int32, Single): this constructor is used to create an instance of the queue class, which is empty, has the specified initial capacity, and uses the specified growth factor.

Let’s see how to create a queue using the queue () constructor:

Step 1: include the system collection and use the namespace in the program with the help of the using keyword.

The syntax is as follows:

using System.Collections;

Step 2: create a queue using the queue class, as shown below:

Queue queue_name = new Queue();

Step 3: if you want to add elements to the queue, use the method of adding elements to the queue. As shown in the following example.

example:

// C# program to illustrate queue
using System;
using System.Collections;
  
public class GFG {
     static public void Main()
     {
  
         // Create a queue
         // Using Queue class
         Queue my_queue = new Queue();
  
         // Adding elements in Queue
         // Using Enqueue() method
         my_queue.Enqueue( "GFG" );
         my_queue.Enqueue(1);
         my_queue.Enqueue(100);
         my_queue.Enqueue( null );
         my_queue.Enqueue(2.4);
         my_queue.Enqueue( "Geeks123" );
  
         // Accessing the elements
         // of my_queue Queue
         // Using foreach loop
         foreach ( var ele in my_queue)
         {
             Console.WriteLine(ele);
         }
     }
}

The output is as follows:

GFG
1
100

2.4
Geeks123

How do I delete an element from a queue?

In a queue, allows you to delete elements from the queue. The queue class provides two different methods to delete elements. These methods are:

Clear: this method is used to delete objects from the queue.

Enqueue: this method deletes the start element of the queue.

example:

// C# program to illustrate how
// to remove elements from queue
using System;
using System.Collections;
  
public class GFG {
     static public void Main()
     {
  
         // Create a queue
         // Using Queue class
         Queue my_queue = new Queue();
  
         // Adding elements in Queue
         // Using Enqueue() method
         my_queue.Enqueue( "GFG" );
         my_queue.Enqueue(1);
         my_queue.Enqueue(100);
         my_queue.Enqueue(2.4);
         my_queue.Enqueue( "Geeks123" );
  
         Console.WriteLine( "Total elements present in my_queue: {0}" , my_queue.Count);
         my_queue.Dequeue();
  
         // After Dequeue method
         Console.WriteLine( "Total elements present in my_queue: {0}" , my_queue.Count);
  
         // Remove all the elements from the queue
         my_queue.Clear();
  
         // After Clear method
         Console.WriteLine( "Total elements present in my_queue: {0}" , my_queue.Count);
     }
}

The output is as follows:

Total elements present in my_queue: 5
Total elements present in my_queue: 4
Total elements present in my_queue: 0

How do I get the highest element of the queue?

In the queue, you can easily find the top element of the queue using the following methods provided by the queue class:

Peek: this method returns the object at the beginning of the queue, but does not delete it.

Enqueue: this method returns the object at the beginning of the modified queue, indicating that this method deletes the top element of the queue.

example:

// C# program to illustrate how
// get topmost elements of the queue
using System;
using System.Collections;
  
public class GFG {
     static public void Main()
     {
  
         // Create a queue
         // Using Queue class
         Queue my_queue = new Queue();
  
         // Adding elements in Queue
         // Using Enqueue() method
         my_queue.Enqueue( "GFG" );
         my_queue.Enqueue( "Geeks" );
         my_queue.Enqueue( "lsbin" );
         my_queue.Enqueue( "geeks" );
         my_queue.Enqueue( "Geeks123" );
  
         Console.WriteLine( "Total elements present in my_queue: {0}" , my_queue.Count);
  
         // Obtain the topmost element of my_queue
         // Using Dequeue method
         Console.WriteLine( "Topmost element of my_queue"
                      + " is: {0}" , my_queue.Dequeue());
                           
  
         Console.WriteLine( "Total elements present in my_queue: {0}" , my_queue.Count);
  
         // Obtain the topmost element of my_queue
         // Using Peek method
         Console.WriteLine( "Topmost element of my_queue is: {0}" , my_queue.Peek());
  
         Console.WriteLine( "Total elements present in my_queue: {0}" , my_queue.Count);
     }
}

The output is as follows:

Total elements present in my_queue: 5
Topmost element of my_queue is: GFG
Total elements present in my_queue: 4
Topmost element of my_queue is: Geeks
Total elements present in my_queue: 4

How do I check the availability of elements in a queue?

In the queue, you can use the contain () method to check whether a given element exists. In other words, if you want to search for elements in a given queue, use the contain () method.

example:

// C# program to illustrate how
// to check element present in
// the queue or not
using System;
using System.Collections;
  
class GFG {
  
     static public void Main()
     {
  
         // Create a queue
         // Using Queue class
         Queue my_queue = new Queue();
  
         // Adding elements in Queue
         // Using Enqueue() method
         my_queue.Enqueue( "GFG" );
         my_queue.Enqueue( "Geeks" );
         my_queue.Enqueue( "lsbin" );
         my_queue.Enqueue( "geeks" );
         my_queue.Enqueue( "Geeks123" );
  
         // Checking if the element is
         // present in the Queue or not
         if (my_queue.Contains( "lsbin" ) == true ) {
             Console.WriteLine( "Element available...!!" );
         }
         else {
             Console.WriteLine( "Element not available...!!" );
         }
     }
}

The output is as follows:

Element available...!!

What is the difference between a general queue and a non general queue?

General queue Non universal queue
Generic queues are defined under the system.collections.generic namespace. Non generic queues are defined under the system. Collections namespace.
A generic queue can only store elements of the same type. Non generic queues can store elements of the same type or different types.
You need to define the type of element in the queue. There is no need to define the type of element in the queue.
This is type safe. It is not type safe.

For more information about c# development, please refer to:Lsbin – it development technologyhttps://www.lsbin.com/

See more c# about:

Recommended Today

I want to discuss canvas 2D and webgl with you

background This article is included inData visualization and graphicsspecial column As mentioned above, I wrote my first column and realized a simple program for the cognition of graphics and visualization. It was originally intended that the follow-up sequence has focused on the algorithm and rendering direction. However, based on the students’ feedback on obscure problems […]