RosettaCodeData/Task/Priority-queue/M2000-Interpreter/priority-queue-2.m2000

58 lines
1.5 KiB
Plaintext

Module PriorityQueue {
a= ((3, "Clear drains"), (4 ,"Feed cat"), ( 5 , "Make tea"))
a=cons(a, ((1 ,"Solve RC tasks"), ( 2 , "Tax return")))
b=stack
comp=lambda (a, b) -> array(a, 0)<array(b, 0)
module InsertPQ (a, n, &comp) {
if len(a)=0 then stack a {data n} : exit
if comp(n, stackitem(a)) then stack a {push n} : exit
stack a {
push n
t=2: b=len(a)
m=b
while t<=b
t1=m
m=(b+t) div 2
if m=0 then m=t1 : exit
If comp(stackitem(m),n) then t=m+1: continue
b=m-1
m=b
end while
if m>1 then shiftback m
}
}
n=each(a)
while n
InsertPq b, array(n), &comp
end while
n1=each(b)
while n1
m=stackitem(n1)
print array(m, 0), array$(m, 1)
end while
\\ Peek topitem (without popping)
print Array$(stackitem(b), 1)
\\ Pop item
Stack b {
Read old
}
print Array$(old, 1)
def Peek$(a)=Array$(stackitem(a), 1)
Function Pop$(a) {
stack a {
=Array$(stackitem(), 1)
drop
}
}
print Peek$(b)
print Pop$(b)
def IsEmpty(a)=len(a)=0
while not IsEmpty(b)
print pop$(b)
end while
}
PriorityQueue