Priority queue: Difference between revisions
Content added Content deleted
Line 4,591:
<syntaxhighlight lang="m2000 interpreter">
// class definitions are global
// if there aren't defintions in a class
global countmany=0&
property toString$ {
value$=format$("{0::-5}"+string$(" ", sp)+"{1:20}", x, s$)
}▼
}▼
class:
}
Module PriorityQueueForGroups {
▲ class obj {
▲ x, s$
▲ class:
▲ module obj (.x, .s$) {}
▲ }
Flush ' empty current stack
Data obj(3, "Clear drains"), obj(4 ,"Feed cat"), obj( 5 , "Make tea
Data obj( 1 ,"Solve RC tasks"), obj( 2 , "Tax return")
ObjectCount()
b=stack
InsertPQ(b) // top of stack is
Print "Using Peek to Examine Priority Queue"
if len(a)=0 then stack a {data n} : exit▼
n1=each(b)
if comp(n, stackitem(a)) then stack a {push n} : exit▼
while n1▼
end while
ObjectCount()
end while
ObjectCount()
// here are the subs/simple functions
// these are static parts of module
sub Header()
Print " Priority Task"
Print "========== ================"
sub ObjectCount()
Print "There are ";countmany;" objects of type obj"
end sub
sub InsertPQ(a, n)
Print "Insert:";n.tostring$(1)
▲ if len(a)=0 then stack a {data n} : exit sub
▲ if @comp(n, stackitem(a)) then stack a {push n} : exit sub
push n
local t=2
local m=b
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
if m>1 then shiftback m
}
function comp(a, b
end function
function Peek$(a as stack)
▲ }
end
▲ Print m.x, m.s$
end function
// Group make a copy
end
▲ =m.s$
▲ drop
▲ }
▲ }
▲ Function IsEmpty(a) {
▲ =len(a)=0
▲ }
▲ While not isEmpty(b) {
▲ Print Pop$(b)
▲ }
}
PriorityQueueForGroups
|