Queue/Definition: Difference between revisions
Content deleted Content added
Line 1,330: | Line 1,330: | ||
(local-put 'make-Q) |
(local-put 'make-Q) |
||
</lang> |
</lang> |
||
=={{header|Elena}}== |
|||
ELENA 3.2.1 : |
|||
<lang elena>import extensions. |
|||
template queue :: type |
|||
{ |
|||
array<type> theArray. |
|||
int theTop. |
|||
int theTale. |
|||
explicit |
|||
[ |
|||
theArray := type&(8). |
|||
theTop := 0. |
|||
theTale := 0. |
|||
] |
|||
// explicit int:capacity |
|||
// [ |
|||
// theArray := list<type>(capacity). |
|||
// 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) |
|||
] |
|||
} |
|||
].</lang> |
|||
{{out}} |
|||
<pre> |
|||
1 |
|||
2 |
|||
3 |
|||
a queue is empty |
|||
Trying to pop:Queue is empty |
|||
</pre> |
|||
=={{header|Elisa}}== |
=={{header|Elisa}}== |