Priority queue: Difference between revisions
added Arturo
m (Fixed indentation issues) |
Drkameleon (talk | contribs) (added Arturo) |
||
Line 1,048:
Priority : 5 : Make tea
</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">define :item [priority, value][
print: [
~"(|this\priority|, |this\value|)"
]
]
define :queue [items][
init: [
this\items: arrange this\items 'it -> it\priority
]
]
empty?: function [this :queue][
zero? this\items
]
push: function [this :queue, item][
this\items: this\items ++ item
this\items: arrange this\items 'it -> it\priority
]
pop: function [this :queue][
ensure -> not? empty? this
result: this\items\0
this\items: remove.index this\items 0
return result
]
Q: to :queue @[to [:item] [
[3 "Clear drains"]
[4 "Feed cat"]
[5 "Make tea"]
[1 "Solve RC tasks"]
]]
push Q to :item [2 "Tax return"]
print ["queue is empty?" empty? Q]
print ""
while [not? empty? Q]->
print ["task:" pop Q]
print ""
print ["queue is empty?" empty? Q]</syntaxhighlight>
{{out}}
<pre>queue is empty? false
task: (1, Solve RC tasks)
task: (2, Tax return)
task: (3, Clear drains)
task: (4, Feed cat)
task: (5, Make tea)
queue is empty? true</pre>
=={{header|ATS}}==
|