How to resolve the algorithm Priority queue step by step in the FunL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Priority queue step by step in the FunL programming language

Table of Contents

Problem Statement

A priority queue is somewhat similar to a queue, with an important distinction: each item is added to a priority queue with a priority level, and will be later removed from the queue with the highest priority element first. That is, the items are (conceptually) stored in the queue in priority order instead of in insertion order.

Create a priority queue.   The queue must support at least two operations:

Optionally, other operations may be defined, such as peeking (find what current top priority/top element is), merging (combining two priority queues into one), etc.

To test your implementation, insert a number of elements into the queue, each with some random priority. Then dequeue them sequentially; now the elements should be sorted by priority. You can use the following task/priority items as input data:

The implementation should try to be efficient.   A typical implementation has   O(log n)   insertion and extraction time,   where   n   is the number of items in the queue.
You may choose to impose certain limits such as small range of allowed priority levels, limited capacity, etc.   If so, discuss the reasons behind it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Priority queue step by step in the FunL programming language

Source code in the funl programming language

import util.ordering
native scala.collection.mutable.PriorityQueue

data Task( priority, description )

def comparator( Task(a, _), Task(b, _) )
  | a > b     = -1
  | a < b     =  1
  | otherwise =  0
  
q = PriorityQueue( ordering(comparator) )

q.enqueue(
  Task(3, 'Clear drains'),
  Task(4, 'Feed cat'),
  Task(5, 'Make tea'),
  Task(1, 'Solve RC tasks'),
  Task(2, 'Tax return')
  )

while not q.isEmpty()
  println( q.dequeue() )

  

You may also check:How to resolve the algorithm Boolean values step by step in the Batch File programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Plain TeX programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the Elixir programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the FreeBASIC programming language