Priority queue: Difference between revisions
Content added Content deleted
m (FreeBASIC moved to the BASIC section.) |
m (→{{header|XPL0}}: Alphabetize. (Very sluggish responses!)) |
||
Line 8,412: | Line 8,412: | ||
[Tax return, 2] |
[Tax return, 2] |
||
[Solve RC tasks, 1] |
[Solve RC tasks, 1] |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
The highest priority item is the one with the minimum number, as in 1st priority. |
|||
<syntaxhighlight lang "XPL0">def PQSize = 10; \Maximum number of items priority queue can hold |
|||
int PQ(PQSize*2), PQI; |
|||
func Remove; \Remove and return item with highest priority |
|||
int Min, I, MinI, Item; |
|||
[if PQI <= 0 then return 0; |
|||
Min:= -1>>1; I:= PQI; |
|||
while I > 0 do |
|||
[I:= I-2; |
|||
if PQ(I) < Min then |
|||
[Min:= PQ(I); |
|||
MinI:= I; |
|||
]; |
|||
]; |
|||
Item:= PQ(MinI+1); \get highest priority Item |
|||
PQI:= PQI-2; |
|||
PQ(MinI):= PQ(PQI); \replace that Item with last item |
|||
PQ(MinI+1):= PQ(PQI+1); |
|||
return Item; |
|||
]; |
|||
proc Insert(Priority, Item); \Insert item into priority queue |
|||
int Priority, Item; |
|||
[if PQI >= PQSize*2 then return; |
|||
PQ(PQI):= Priority; |
|||
PQ(PQI+1):= Item; |
|||
PQI:= PQI+2; |
|||
]; |
|||
int Items, I; |
|||
[Items:= [ |
|||
[3, "Clear drains"], |
|||
[4, "Feed cat"], |
|||
[5, "Make tea"], |
|||
[1, "Solve RC tasks"], |
|||
[2, "Tax return"] ]; |
|||
PQI:= 0; |
|||
for I:= 0 to 5-1 do |
|||
Insert(Items(I,0), Items(I,1)); |
|||
for I:= 0 to 5-1 do |
|||
[Text(0, Remove); CrLf(0)]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Solve RC tasks |
|||
Tax return |
|||
Clear drains |
|||
Feed cat |
|||
Make tea |
|||
</pre> |
</pre> |
||
Line 8,572: | Line 8,519: | ||
()</syntaxhighlight> |
()</syntaxhighlight> |
||
=={{header|XPL0}}== |
|||
The highest priority item is the one with the minimum number, as in 1st priority. |
|||
<syntaxhighlight lang "XPL0">def PQSize = 10; \Maximum number of items priority queue can hold |
|||
int PQ(PQSize*2), PQI; |
|||
func Remove; \Remove and return item with highest priority |
|||
int Min, I, MinI, Item; |
|||
[if PQI <= 0 then return 0; |
|||
Min:= -1>>1; I:= PQI; |
|||
while I > 0 do |
|||
[I:= I-2; |
|||
if PQ(I) < Min then |
|||
[Min:= PQ(I); |
|||
MinI:= I; |
|||
]; |
|||
]; |
|||
Item:= PQ(MinI+1); \get highest priority Item |
|||
PQI:= PQI-2; |
|||
PQ(MinI):= PQ(PQI); \replace that Item with last item |
|||
PQ(MinI+1):= PQ(PQI+1); |
|||
return Item; |
|||
]; |
|||
proc Insert(Priority, Item); \Insert item into priority queue |
|||
int Priority, Item; |
|||
[if PQI >= PQSize*2 then return; |
|||
PQ(PQI):= Priority; |
|||
PQ(PQI+1):= Item; |
|||
PQI:= PQI+2; |
|||
]; |
|||
int Items, I; |
|||
[Items:= [ |
|||
[3, "Clear drains"], |
|||
[4, "Feed cat"], |
|||
[5, "Make tea"], |
|||
[1, "Solve RC tasks"], |
|||
[2, "Tax return"] ]; |
|||
PQI:= 0; |
|||
for I:= 0 to 5-1 do |
|||
Insert(Items(I,0), Items(I,1)); |
|||
while PQI > 0 do |
|||
[Text(0, Remove); CrLf(0)]; |
|||
]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Solve RC tasks |
|||
Tax return |
|||
Clear drains |
|||
Feed cat |
|||
Make tea |
|||
</pre> |
|||
=={{header|Zig}}== |
=={{header|Zig}}== |