Queue/Definition: Difference between revisions

Content added Content deleted
Line 1,331: Line 1,331:
</lang>
</lang>
=={{header|Elena}}==
=={{header|Elena}}==
ELENA 3.2.1 :
ELENA 3.3 :
<lang elena>import extensions.
<lang elena>import extensions.

template queue :: type
template queue :: type
{
{
Line 1,339: Line 1,339:
int theTop.
int theTop.
int theTale.
int theTale.
explicit
explicit
[
[
theArray := type&(8).
theArray := type<>(8).
theTop := 0.
theTop := 0.
theTale := 0.
theTale := 0.
]
]
bool empty
bool empty
= theTop == theTale.
= theTop == theTale.
push type:anObject
push type:anObject
[
[
Line 1,356: Line 1,356:
theArray := theArray reallocate(theTale).
theArray := theArray reallocate(theTale).
].
].
theArray[theTale] := anObject.
theArray[theTale] := anObject.
theTale += 1.
theTale += 1.
]
]
type pop
type pop
[
[
if (theTale == theTop)
if (theTale == theTop)
[ InvalidOperationException new:"Queue is empty"; raise ].
[ InvalidOperationException new:"Queue is empty"; raise ].
type item := theArray[theTop].
type item := theArray[theTop].
theTop += 1.
theTop += 1.
^ item
^ item
]
]
}
}

program =
program =
[
[
queue<int> q := queue<int>.
queue<int> q := queue<int>().
q push(1).
q push(1).
q push(2).
q push(2).