Queue/Definition: Difference between revisions
Content deleted Content added
→{{header|Ruby}}: Add RDoc comments and a few more aliases. Usage remains as before. |
|||
Line 822: | Line 822: | ||
return [reader, writer] |
return [reader, writer] |
||
}</lang> |
}</lang> |
||
=={{header|Elisa}}== |
|||
This is a generic Queue component based on bi-directional lists. See how in Elisa these [http://jklunder.home.xs4all.nl/elisa/part01/doc080.html lists] are defined. |
|||
<lang Elisa> |
|||
component GenericQueue ( Queue, Element ); |
|||
type Queue; |
|||
Queue (MaxLength = integer) -> Queue; |
|||
Length( Queue ) -> integer; |
|||
Empty ( Queue ) -> boolean; |
|||
Full ( Queue ) -> boolean; |
|||
Push ( Queue, Element) -> nothing; |
|||
Pull ( Queue ) -> Element; |
|||
begin |
|||
Queue (MaxLength) = Queue:[ MaxLength; length:=0; list=alist(Element) ]; |
|||
Length ( queue ) = queue.length; |
|||
Empty ( queue ) = (queue.length <= 0); |
|||
Full ( queue ) = (queue.length >= queue.MaxLength); |
|||
Push ( queue, element ) = |
|||
[ exception (Full(queue), "Queue Overflow"); |
|||
queue.length:= queue.length + 1; |
|||
add (queue.list, element)]; |
|||
Pull ( queue ) = |
|||
[ exception (Empty(queue), "Queue Underflow"); |
|||
queue.length:= queue.length - 1; |
|||
remove(first(queue.list))]; |
|||
end component GenericQueue; |
|||
</lang> |
|||
In the following tests we will also show how the internal structure of the queue can be made visible to support debugging. |
|||
<lang Elisa> |
|||
use GenericQueue (QueueofPersons, Person); |
|||
type Person = text; |
|||
Q = QueueofPersons(25); |
|||
Push (Q, "Peter"); |
|||
Push (Q, "Alice"); |
|||
Push (Q, "Edward"); |
|||
Q? |
|||
QueueofPersons:[MaxLength = 25; |
|||
length = 3; |
|||
list = { "Peter", |
|||
"Alice", |
|||
"Edward"}] |
|||
Pull (Q)? |
|||
"Peter" |
|||
Pull (Q)? |
|||
"Alice" |
|||
Pull (Q)? |
|||
"Edward" |
|||
Q? |
|||
QueueofPersons:[MaxLength = 25; |
|||
length = 0; |
|||
list = { }] |
|||
Pull (Q)? |
|||
***** Exception: Queue Underflow |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |