64 lines
1.0 KiB
Plaintext
64 lines
1.0 KiB
Plaintext
import extensions;
|
|
|
|
class queue<T>
|
|
{
|
|
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)
|
|
{
|
|
theArray := theArray.reallocate(theTale)
|
|
};
|
|
|
|
theArray[theTale] := object;
|
|
|
|
theTale += 1
|
|
}
|
|
|
|
T pop()
|
|
{
|
|
if (theTale == theTop)
|
|
{ InvalidOperationException.new("Queue is empty").raise() };
|
|
|
|
T item := theArray[theTop];
|
|
|
|
theTop += 1;
|
|
|
|
^ item
|
|
}
|
|
}
|
|
|
|
public program()
|
|
{
|
|
queue<int> q := new 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()
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
console.printLine(e.Message)
|
|
}
|
|
}
|