Queue/Definition: Difference between revisions

PascalABC.NET
(PascalABC.NET)
 
Line 4,842:
writeln('Testing finished.')
end.</syntaxhighlight>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
type
Node<T> = auto class
data: T;
next: Node<T>;
end;
MyQueue<T> = class
head,tail: Node<T>;
public
procedure Enqueue(x: T);
begin
if tail = nil then
begin
tail := new Node<T>(x,nil);
head := tail;
end
else
begin
tail.next := new Node<T>(x,nil);
tail := tail.next;
end;
end;
function Dequeue(): T;
begin
Result := head.data;
head := head.Next;
if head = nil then
tail := nil;
end;
function IsEmpty: boolean := head = nil;
end;
 
begin
var q := new MyQueue<integer>;
for var i:=1 to 5 do
q.Enqueue(i);
while not q.IsEmpty do
Print(q.Dequeue);
Print(q.IsEmpty);
end.
</syntaxhighlight>
{{out}}
<pre>
1 2 3 4 5 True
</pre>
 
 
=={{header|Perl}}==
246

edits