Priority queue: Difference between revisions

Content added Content deleted
Line 4,779: Line 4,779:
flush
flush
// subs can read from module's stack
// subs can read from module's stack
println( "Add items to pq queue")
println("Add items to pq queue")
Data 4 ,"Feed cat",5 , "Make tea", 3, "Clear drains",1 , "Solve RC tasks"
Data 4 ,"Feed cat",5 , "Make tea", 3, "Clear drains",1 , "Solve RC tasks"
AddItems(pq)
AddItems(pq)
println("Add items to zz queue")
println("Add items to zz queue")
AddItems(zz, 2 , "Tax return", 1 ,"Solve RC tasks#2")
AddItems(zz, 2 , "Tax return", 1 ,"Solve RC tasks#2")
println( "Peek top from zz queue")
println("Peek top from zz queue")
PeekTop(zz) // Solve RC tasks#2
PeekTop(zz) // Solve RC tasks#2
println( "Merge two priority lists")
println("Merge two priority lists")
merge(pq, zz, false)
merge(pq, zz, false)
println("Peek top from pq queue")
println("Peek top from pq queue")
Line 4,798: Line 4,798:
PopOne(pq)
PopOne(pq)
end while
end while
println( "Pop one from zz until empty queue")
println("Pop one from zz until empty queue")
while len(zz)>0
while len(zz)>0
PopOne(zz)
PopOne(zz)
Line 4,816: Line 4,816:
sub merge(pq, qp, emptyqueue)
sub merge(pq, qp, emptyqueue)
local needsort=false
local needsort=false
local kqp=each(qp, -1, 1), k$
local kqp=each(qp, -1, 1), k$, t, p
while kqp
while kqp
local t=eval(kqp)
t=eval(kqp)
k$= eval$(kqp!)
k$= eval$(kqp!)
if not exist(pq, eval$(kqp!)) then
if not exist(pq, eval$(kqp!)) then
Line 4,825: Line 4,825:
needsort=true
needsort=true
else
else
local p=eval(pq)
p=eval(pq)
end if
end if
stack p {
stack p {
if emptyqueue then
if emptyqueue then