Priority queue: Difference between revisions

Add SenseTalk implementation
(→‎{{header|C sharp}}: .NET 6 version)
(Add SenseTalk implementation)
Line 5,903:
def compare(t1:Task, t2:Task):Int=t2.prio compare t1.prio
}</lang>
 
=={{header|SenseTalk}}==
We use New to create an object instance -- in this case (for simplicity) based on the script itself, which is named PriorityQueue. The Tell command or dot notation are then used to send messages directly to that object.
<lang sensetalk>
// PriorityQueue.script
set Tasks to a new PriorityQueue
 
tell Tasks to add 3,"Clear drains"
tell Tasks to add 4,"Feed cat"
tell Tasks to add 5,"Make tea"
tell Tasks to add 1,"Solve RC tasks"
tell Tasks to add 2,"Tax return"
 
put "Initial tasks:"
put Tasks.report & return
 
put Tasks.getTop into topItem
put "Top priority: " & topItem & return
 
put "Remaining:" & return & Tasks.report
 
------
properties
queue:[]
end properties
 
to add newPriority, newTask
repeat with each item of my queue
if newPriority comes before the priority of it then
insert {priority:newPriority, task:newTask} before item the counter of my queue
return
end if
end repeat
insert {priority:newPriority, task:newTask} into my queue -- add at end if last priority
end add
 
to getTop
pull my queue into firstItem
return firstItem
end getTop
 
to report
return (priority of each && task of each for each item of my queue) joined by return
end report
</lang>
{{out}}
<pre>
Initial tasks:
1 Solve RC tasks
2 Tax return
3 Clear drains
4 Feed cat
5 Make tea
 
Top priority: {priority:1, task:"Solve RC tasks"}
 
Remaining:
2 Tax return
3 Clear drains
4 Feed cat
5 Make tea
</pre>
 
=={{header|Sidef}}==