Friday, July 15, 2022
HomeITLearn how to work with a precedence queue in .NET 6

Learn how to work with a precedence queue in .NET 6


A queue is a knowledge construction that works on a primary in, first out (FIFO) foundation. Gadgets are inserted on the rear of the queue and faraway from the entrance. The time period “enqueue” denotes the operation that inserts information within the queue, whereas the time period “dequeue” denotes the operation that removes information from the queue.

A precedence queue is a sort of queue by which the weather are organized primarily based on precedence values you assign to them. Help for a precedence queue was newly launched in .NET 6. This text will focus on the PriorityQueue class in .NET 6 and how one can work with it in our .NET 6 functions.

To work with the code examples offered on this article, it is best to have Visible Studio 2022 put in in your system. In the event you don’t have already got a duplicate, you may obtain Visible Studio 2022 right here.

Create a console software mission in Visible Studio

First off, let’s create a .NET Core Console Software mission in Visible Studio. Assuming Visible Studio 2022 is put in in your system, observe the steps outlined under to create a brand new .NET Core Console Software mission in Visible Studio.

  1. Launch the Visible Studio IDE.
  2. Click on on “Create new mission.”
  3. Within the “Create new mission” window, choose “Console App (.NET Core)” from the checklist of templates displayed.
  4. Click on Subsequent.
  5. Within the “Configure your new mission” window proven subsequent, specify the title and site for the brand new mission.
  6. Click on Create.

We’ll use this mission to work with a precedence queue within the subsequent sections of this text.

Create a precedence queue in .NET 6

A queue in .NET 6 is often a FIFO information construction, by which objects are added to the rear of the queue and faraway from the entrance. A precedence queue in .NET 6 is a particular kind of queue that orders the objects primarily based on the precedence values you assign to them.

You may create an occasion of the PriorityQueue class utilizing the constructor of the PriorityQueue class as proven under.

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

Upon getting created a PriorityQueue, you may add objects to the queue utilizing the Enqueue() methodology. The Enqueue methodology accepts two parameters—the aspect to be added as string and the precedence of the aspect as an integer.

Notice that objects in a precedence queue are organized within the descending order of their precedence values. Therefore the merchandise having the most important precedence worth (e.g., 9) is positioned on the rear of the queue, and the merchandise having the bottom precedence worth (e.g., 0) is positioned on the entrance. In different phrases, a dequeue removes the merchandise with the bottom precedence worth.

The next code snippet reveals how one can add objects to a precedence queue utilizing the Enqueue methodology.

PriorityQueue<string, int> priorityQueue = new PriorityQueue<string, int>();
priorityQueue.Enqueue("Merchandise A", 4);
priorityQueue.Enqueue("Merchandise B", 3);
priorityQueue.Enqueue("Merchandise C", 2);
priorityQueue.Enqueue("Merchandise D", 6);
priorityQueue.Enqueue("Merchandise E", 7);
priorityQueue.Enqueue("Merchandise F", 5);
priorityQueue.Enqueue("Merchandise G", 0);
priorityQueue.Enqueue("Merchandise H", 9);
priorityQueue.Enqueue("Merchandise I", 1);
priorityQueue.Enqueue("Merchandise J", 8);

Retrieve parts from a precedence queue in .NET 6

You may retrieve objects from a PriorityQueue in two alternative ways. One choice is to make use of the Dequeue() methodology, which returns the merchandise having the bottom precedence worth within the queue. The opposite choice is to make use of the Peek() methodology, which returns the merchandise having the bottom precedence worth with out eradicating it from the queue.

The TryDequeue and TryPeek strategies are enhanced variations of the Dequeue and Peek strategies that deal with exceptions internally. They return true if an merchandise has been efficiently faraway from the queue, and return false in any other case.

The next code snippet illustrates how one can take away the objects from the precedence queue and show every merchandise and its precedence on the console window.

whereas (priorityQueue.TryDequeue(out string queueItem, out int precedence))
{
     Console.WriteLine($"Merchandise : {queueItem}. Precedence : {precedence}");
}

Full precedence queue instance in .NET 6

The entire program is given under in your reference.

utilizing System.Collections.Generic;
inside class Program
{
   static void Most important(string[] args)
   {
     PriorityQueue<string, int> priorityQueue =
     new PriorityQueue<string, int>();
     priorityQueue.Enqueue("Merchandise A", 4);
     priorityQueue.Enqueue("Merchandise B", 3);
     priorityQueue.Enqueue("Merchandise C", 2);
     priorityQueue.Enqueue("Merchandise D", 6);
     priorityQueue.Enqueue("Merchandise E", 7);
     priorityQueue.Enqueue("Merchandise F", 5);
     priorityQueue.Enqueue("Merchandise G", 0);
     priorityQueue.Enqueue("Merchandise H", 9);
     priorityQueue.Enqueue("Merchandise I", 1);
     priorityQueue.Enqueue("Merchandise J", 8);

     whereas (priorityQueue.TryDequeue(out string queueItem, out int precedence))
     {
         Console.WriteLine($"Merchandise : {queueItem}. Precedence : {precedence}");
     }
     Console.Learn();
   }
}

Once you execute the appliance, the console output ought to seem as proven in Determine 1 under.

dotnet priority queue 01 IDG

Determine 1. Dequeueing our precedence queue.

Depend the weather in a precedence queue in .NET 6

You need to use the next code snippet to test the variety of objects within the precedence queue at any given level of time.

int ctr = priorityQueue.Depend;
Console.WriteLine($"No of things remaining within the precedence queue : {ctr}");

In the event you insert these strains of code after the whereas assertion in our program, as illustrated under, the variety of out there objects within the precedence queue shall be 0. It’s because every name to the TryDequeue methodology removes a component from the precedence queue.

whereas (priorityQueue.TryDequeue(out string queueItem, out int precedence))
{
    Console.WriteLine($"Merchandise : {queueItem}. Precedence : {precedence}");
}
int ctr = priorityQueue.Depend;
Console.WriteLine($"No of things remaining within the precedence queue : {ctr}");

In the event you run our program with the code above, it should show the objects of the precedence queue and their precedence values. Lastly, it should print a worth 0, which denotes the entire variety of parts remaining within the precedence queue on the finish.

dotnet priority queue 02 IDG

Determine 2. Our precedence queue with aspect counter.

A precedence queue in .NET 6 makes use of the IComparer interface to find out the precedence of parts saved inside it. You may write your personal implementation of the IComparer interface and use it to find out the priorities of parts in a precedence queue. I’ll focus on this additional in a future put up right here.

Lastly, notice {that a} PriorityQueue occasion shouldn’t be thread-safe. It’s best to write your personal customized code to deal with thread security to keep away from race circumstances. Precedence queues are usually utilized by working programs for load balancing, thread scheduling, and dealing with interrupts. An working system will use a precedence queue to retailer threads and schedule or preempt them as wanted.

Copyright © 2022 IDG Communications, Inc.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments