Jump to content

Priority queue: Difference between revisions

m
Line 4,591:
 
<syntaxhighlight lang="m2000 interpreter">
// class definitions are global
// if there aren't defintions in a class
global countmany=0&
class obj {
x, s$
property toString$ {
} value (sp=8) {
=m.link parent x, s$ to x, s$
value$=format$("{0::-5}"+string$(" ", sp)+"{1:20}", x, s$)
} }
}
class:remove {
dropcountmany--
}
class:
module obj (.x, .s$) {countmany++}
}
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"), obj( 1 ,"Solve RC tasks"), obj( 2 , "Tax return")
Data obj( 1 ,"Solve RC tasks"), obj( 2 , "Tax return")
ObjectCount()
b=stack
comp=lambdawhile (a,not b) ->{empty
InsertPQ(b) // top of stack is =a.x<b.x then objects follow
}end while
module InsertPQ ObjectCount(a, n, &comp) {
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
stack a {Header()
while n1
Print Pop@Peek$(bn1)
end while
ObjectCount()
}Header()
Whilewhile not @isEmpty(b) {
Print m.x, m.s@Pop(b)=>tostring$
end while
ObjectCount()
// here are the subs/simple functions
// these are static parts of module
sub Header()
Print " Priority Task"
Print "========== ================"
}end sub
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
stack =len(a)=0 {
push n
local t=2:, b=len(a)
local 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
}
}end sub
function comp(a, b=stack)
While not empty { =a.x<b.x
end function
InsertPQ b, Group, &comp ' Group pop a group from current stack
function Peek$(a as stack)
}
n1 =eachstackitem(ba)=>toString$
while n1 { countmany++
end m=stackitem(n1)function
Functionfunction IsEmpty(a) {
Print m.x, m.s$
} =len(a)=0
end function
Function Peek$(a) {m=stackitem(a) : =m.s$}
PrintFunction Peek$Pop(ba)
// Group make a copy
Function Pop$( stack a) {=Group:countmany++}
end stack a {function
m=stackitem()
=m.s$
drop
}
}
Function IsEmpty(a) {
=len(a)=0
}
While not isEmpty(b) {
Print Pop$(b)
}
}
PriorityQueueForGroups
404

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.