module priority_queue_mod implicit none type node character (len=100) :: task integer :: priority end type type queue type(node), allocatable :: buf(:) integer :: n = 0 contains procedure :: top procedure :: enqueue procedure :: siftdown end type contains subroutine siftdown(this, a) class (queue) :: this integer :: a, parent, child associate (x => this%buf) parent = a do while(parent*2 <= this%n) child = parent*2 if (child + 1 <= this%n) then if (x(child+1)%priority > x(child)%priority ) then child = child +1 end if end if if (x(parent)%priority < x(child)%priority) then x([child, parent]) = x([parent, child]) parent = child else exit end if end do end associate end subroutine function top(this) result (res) class(queue) :: this type(node) :: res res = this%buf(1) this%buf(1) = this%buf(this%n) this%n = this%n - 1 call this%siftdown(1) end function subroutine enqueue(this, priority, task) class(queue), intent(inout) :: this integer :: priority character(len=*) :: task type(node) :: x type(node), allocatable :: tmp(:) integer :: i x%priority = priority x%task = task this%n = this%n +1 if (.not.allocated(this%buf)) allocate(this%buf(1)) if (size(this%buf)0) x = q%top() print "(g0,a,a)", x%priority, " -> ", trim(x%task) end do end program ! Output: ! 5 -> Make Tea ! 4 -> Feed cat ! 3 -> Clear drains ! 2 -> Tax return ! 1 -> Solve RC tasks