unit mTList;
{ Version vom 07.11.2008 }
{ Die Klassendefinition entspricht den Vorgaben zu "TList" aus:
"Materialien zu den zentralen Abiturprüfungen im Fach Informatik
objektorientierter Ansatz, Delphi" NRW 2008, Seite 10: "Dokumentation
der Klasse TList"
Die Liste ist doppelt verkettet mit einem Dummy-Element vor und einem
Dummy-Element hinter der Liste. }
interface
uses STDCtrls, SysUtils, Forms;
Type TListNode =
class (TObject)
private
zItem: TObject;
zPrevious, zNext: TListNode;
protected
constructor Create(Item: TObject; Previous, Next: TListNode);
virtual;
destructor Destroy;
virtual;
procedure SetNext (Next: TListNode);
procedure SetPrevious (Previous: TListNode);
procedure SetItem (Item: TObject);
function GetPrevious: TListNode;
function GetNext: TListNode;
function GetItem: TObject;
public
// TListNode ist eine Hilfsklasse für die in dieser UNIT deklarierten Klasse
// TList. Daher werden keine öffentlichen Methoden angeboten!
end;
Type TList =
class (TObject)
private
zCurrent, zDummyVorne, zDummyHinten: TListNode;
public
constructor Create;
virtual;
function IsEmpty: boolean;
virtual;
function IsInFrontOf: boolean;
virtual;
function IsBehind: boolean;
virtual;
procedure Next;
virtual;
procedure Previous;
virtual;
procedure ToFirst;
virtual;
procedure ToLast;
virtual;
function GetItem: TObject;
virtual;
procedure Replace (pObject: TObject);
virtual;
procedure InsertInFrontOf(pObject: TObject);
virtual;
procedure InsertBehind (pObject: TObject);
virtual;
procedure Remove;
virtual;
procedure AddList(pList: TList);
destructor Destroy;
virtual;
end;
implementation
{ ------------------------ TListNode ---------------------------------------- }
constructor TListNode.Create(Item: TObject; Previous, Next: TListNode);
begin
inherited Create;
zItem:= Item;
zPrevious:= Previous;
zNext:= Next;
end;
destructor TListNode.Destroy;
begin
zItem:=
nil;
zPrevious:=
nil;
zNext:=
nil;
inherited Destroy;
end;
function TListNode.GetItem: TObject;
begin
result:= zItem;
end;
procedure TListNode.SetItem(Item: TObject);
begin
zItem:= Item;
end;
function TListNode.GetPrevious;
begin
result:= zPrevious;
end;
procedure TListNode.SetPrevious(Previous: TListNode);
begin
zPrevious:= Previous;
end;
function TListNode.GetNext;
begin
result:= zNext;
end;
procedure TListNode.SetNext(Next: TListNode);
begin
zNext:= Next;
end;
{ ------------------------------------ TList ------------------------------- }
constructor TList.Create;
begin
inherited Create;
zDummyVorne:= TListNode.Create(
nil,
nil,
nil);
zDummyHinten:= TListNode.Create(
nil, zDummyVorne,
nil);
zDummyVorne.SetNext(zDummyHinten);
zCurrent:= zDummyVorne;
end;
function TList.IsEmpty: boolean;
begin
result:= zDummyVorne.zNext = zDummyHinten;
end;
function TList.IsInFrontOf: boolean;
begin
result:= zCurrent = zDummyVorne;
end;
function TList.IsBehind: boolean;
begin
result:= zCurrent = zDummyHinten;
end;
procedure TList.Next;
begin
if not IsBehind
then zCurrent:= zCurrent.GetNext;
end;
procedure TList.Previous;
begin
if not IsInFrontOf
then zCurrent:= zCurrent.GetPrevious;
end;
procedure TList.ToFirst;
begin
zCurrent:= zDummyVorne.zNext;
end;
procedure TList.ToLast;
begin
zCurrent:= zDummyHinten.zPrevious;
end;
function TList.GetItem: TObject;
begin
result:= zCurrent.GetItem;
end;
procedure TList.Replace(pObject: TObject);
begin
if not (IsBehind
or IsInFrontOf)
then zCurrent.SetItem(pObject);
end;
procedure TList.InsertInFrontOf(pObject: TObject);
var new: TListNode;
begin
if not IsInFrontOf
then begin
new:= TListNode.Create(pObject, zCurrent.zPrevious, zCurrent);
zCurrent.zPrevious.zNext:= new;
zCurrent.zPrevious:= new;
end;
end;
procedure TList.InsertBehind(pObject: TObject);
var new: TListNode;
begin
if not IsBehind
then begin
new:= TListNode.Create(pObject, zCurrent, zCurrent.zNext);
zCurrent.zNext.zPrevious:= new;
zCurrent.zNext:= new;
end;
end;
procedure TList.Remove;
var help: TListNode;
begin
if not (IsInFrontOf
or IsBehind)
then begin
help:= zCurrent;
zCurrent.zPrevious.SetNext(zCurrent.zNext);
zCurrent.zNext.SetPrevious(zCurrent.zPrevious);
zCurrent:= help.zNext;
help.Destroy;
end;
end;
procedure TList.AddList(pList: TList);
begin
(*** ist noch zu implementieren !!! ***)
end;
destructor TList.Destroy;
begin
ToFirst;
while not IsEmpty
do remove;
zDummyVorne.Destroy;
zDummyHinten.Destroy;
inherited Destroy;
end;
end.