June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
62
Task/Queue-Definition/Elena/queue-definition.elena
Normal file
62
Task/Queue-Definition/Elena/queue-definition.elena
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import extensions.
|
||||
|
||||
template queue :: type
|
||||
{
|
||||
array<type> theArray.
|
||||
int theTop.
|
||||
int theTale.
|
||||
|
||||
explicit
|
||||
[
|
||||
theArray := type<>(8).
|
||||
theTop := 0.
|
||||
theTale := 0.
|
||||
]
|
||||
|
||||
bool empty
|
||||
= theTop == theTale.
|
||||
|
||||
push type:anObject
|
||||
[
|
||||
if (theTale > theArray length)
|
||||
[
|
||||
theArray := theArray reallocate(theTale).
|
||||
].
|
||||
|
||||
theArray[theTale] := anObject.
|
||||
|
||||
theTale += 1.
|
||||
]
|
||||
|
||||
type pop
|
||||
[
|
||||
if (theTale == theTop)
|
||||
[ InvalidOperationException new:"Queue is empty"; raise ].
|
||||
|
||||
type item := theArray[theTop].
|
||||
|
||||
theTop += 1.
|
||||
|
||||
^ item
|
||||
]
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
queue<int> q := queue<int>().
|
||||
q push(1).
|
||||
q push(2).
|
||||
q push(3).
|
||||
console printLine(q pop).
|
||||
console printLine(q pop).
|
||||
console printLine(q pop).
|
||||
console printLine("a queue is ", q empty; iif("empty","not empty")).
|
||||
console print("Trying to pop:").
|
||||
try(q pop)
|
||||
{
|
||||
on(Exception e)
|
||||
[
|
||||
console printLine(e message)
|
||||
]
|
||||
}
|
||||
].
|
||||
Loading…
Add table
Add a link
Reference in a new issue