RosettaCodeData/Task/Queue-Definition/Elena/queue-definition.elena

67 lines
1.2 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import extensions;
2026-02-01 16:33:20 -08:00
class Queue<T>
2023-07-01 11:58:00 -04:00
{
T[] theArray;
int theTop;
int theTale;
constructor()
{
theArray := new T[](8);
theTop := 0;
theTale := 0;
}
bool empty()
= theTop == theTale;
push(T object)
{
if (theTale > theArray.Length)
{
2026-02-01 16:33:20 -08:00
auto newArray := class Array<T>.allocate(theTale);
class Array<T>.copy(newArray, theArray, 0, theArray.Length);
theArray := newArray
2023-07-01 11:58:00 -04:00
};
theArray[theTale] := object;
theTale += 1
}
T pop()
{
if (theTale == theTop)
2024-03-06 22:25:12 -08:00
{ InvalidOperationException.new("Queue is empty").raise() };
2023-07-01 11:58:00 -04:00
T item := theArray[theTop];
theTop += 1;
^ item
}
}
2026-02-01 16:33:20 -08:00
public Program()
2023-07-01 11:58:00 -04:00
{
2026-02-01 16:33:20 -08:00
Queue<int> q := new Queue<int>();
2023-07-01 11:58:00 -04:00
q.push(1);
q.push(2);
q.push(3);
2026-02-01 16:33:20 -08:00
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:");
2023-07-01 11:58:00 -04:00
try
{
q.pop()
}
catch(Exception e)
{
2026-02-01 16:33:20 -08:00
Console.printLine(e.Message)
2023-07-01 11:58:00 -04:00
}
}