Learn about Priority Queue in C# with examples

As we learned in the previous section about queues, here is the link to brush up on queue techniques. https://www.c-sharpcorner.com/article/queue-in-c-sharp/

Introduction

In computer science, priority queues are a crucial type of data structure that allow for the effective administration of jobs with different levels of urgency. This data structure can be implemented using the PriorityQueue class that is provided by the System.Collections.Generic namespace.

What is the Priority Queue in C#?

One kind of data structure that arranges elements according to priority is called a priority queue. An element's priority is usually established by the key that is connected to it. Depending on the application, the key could be an integer, a floating-point number, or even a text.

The highest priority element is always returned first in a priority queue, which is its main characteristic. An element is positioned according to its priority when it is added to the Priority Queue. The element with the highest priority is the one that is always removed from a Priority Queue.

PriorityQueue Methods

The PriorityQueue class in C# offers several methods for working with the Priority Queue. The following are a few of the most popular techniques:

1. Enqueue

An item can be added or inserted into the Priority Queue using the Enqueue method. The highest priority item is added first, then other items are added in order of priority. The item to be added to the Priority Queue is the only input accepted by the Enqueue method. As an illustration, consider this:

PriorityQueue<string, int> priorityQueue = new PriorityQueue<string, int>();

priorityQueue.Enqueue("Jaimin", 1);

Enqueue

In the above example, I have added elements with the priority, and when we are fetching the data from the priority queue, it will get the data based on the priority as we associate.

2. Dequeue

The item from the Priority Queue with the highest priority is returned by the Dequeue method, which is used to delete or remove items. This method will throw an InvalidOperationException if the Priority Queue is empty. As an illustration, consider this:

priorityQueue.Dequeue();

Dequeue

3. Peek

Without deleting it, the Peek function retrieves the item from the Priority Queue with the highest priority. This method will throw an InvalidOperationException if the Priority Queue is empty. As an illustration, consider this:

priorityQueue.Peek();

Peek

In the above example, I have used the couple of time Peek method, and the output will be the same.

4. Count

The number of entries in the Priority Queue may be found using the Count attribute. As an illustration, consider this:

priorityQueue.Count;

Count

5. Clear

All entries in the Priority Queue are removed using the Clear technique. As an illustration, consider this:

priorityQueue.Clear();

Clear

6. Ensure Capacity

makes certain that this queue has a capacity that is at least the designated capacity. It is upgraded to at least the designated capacity if the present capacity is less than capacity.

priorityQueue.EnsureCapacity(0);

Ensure capacity 

In the above example, we are dequeuing the elements from the priority queue and using the TrimExcess method to remove the unused elements from the priority queue. After checking the EnsureCapacity method, it will increase the size of the priority queue by 5. Whenever we check the ensure capacity method in the priority queue, if the size of the elements is less than that, it will increase the size of the four with the actual data.

7. Trim Excess

If the current capacity is less than 90%, the capacity is set to the actual number of items in the PriorityQueue<TElement,TPriority>.

priorityQueue.TrimExcess();

Trim access

8. EnqueueDequeue

Adds the specified element with associated priority to the and immediately removes the minimal element, returning the result.

priorityQueue.EnqueueDequeue(“Jaimin”, 0);

Enqueue_dequeue

9. EnqueueRange

Enumerates a series of element pairs that are all connected to the given priority.

priorityQueue.EnsureRange(new string[] { “Dwisha”, “Dwiti” }, 0);

Enqueue Range

We learned the new technique and evolved together.

Happy coding. :)


Similar Articles