Delphi-Quellcode:
program ListTest;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
pNode = ^tNode;
tNode = record
data: integer;
next: pNode;
end;
var
FirstNode: pNode;
AktNode : pNode;
function Traverse(p: pNode): pNode;
begin
Result := p.next;
end;
procedure KillList;
var
temp: pNode;
begin
temp := FirstNode;
AktNode := FirstNode;
While Temp <> NIL do
begin
AktNode := Traverse(AktNode);
dispose(Temp);
temp := AktNode;
end;
end;
function AddNode(p: pNode): pNode;
begin
if FirstNode = NIL then
begin
FirstNode := p;
end
else
begin
p.next := FirstNode;
FirstNode := p;
end;
AktNode := p;
result := AktNode;
end;
var
i: integer;
t: pNode;
begin
FirstNode := NIL;
AktNode := NIL;
writeln('AddNodes ...');
for i := 0 to 3 do
begin
New(t);
t.next := NIL;
t.data := i; //Daten zuweisen
AktNode := AddNode(t);
Writeln('Node mit Data: ', AktNode.data);
end;
writeln;
writeln('TraversNodes...');
AktNode := FirstNode;
while AktNode <> NIL do
begin
Writeln('Node mit Data: ', AktNode.data);
AktNode := Traverse(Aktnode); //Geht zum nächsten Knoten
end;
KillList; //Löscht die Verkettete Liste
readln;
end.
hier mal 'n kleines demo progy <HTH>