type
PStackRec=^TStackRec;
TStackRec=record
Data:char;
Next
StackRec;
Prev
StackRec;
end;
TStack=class
private
pHead
StackRec;
protected
procedure FreeAllNode;
public
constructor Create;
destructor Destroy;
override;
function IsEmpty:boolean;
function GetData(var a_ch:char):boolean;
function PopData(var a_ch:char):boolean;
function PushData(a_ch:char):boolean;
end;
implementation
{TStack}
constructor TStack.Create;
begin
GetMem(pHead,SizeOf(TStackRec));
pHead^.Next:=pHead;
pHead^.Prev:=pHead;
inherited;
end;
procedure TStack.FreeAllNode;
var
pNode
StackRec;
begin
while pHead^.Next<>pHeaddo
begin
pNode:=pHead^.Next;
pHead^.Next:=pNode^.Next;
FreeMem(pNode);
end;
pHead^.Next:=pHead;
pHead^.Prev:=pHead;
end;
destructor TStack.Destroy;
begin
FreeAllNode;
FreeMem(pHead);
inherited;
end;
function TStack.IsEmpty:boolean;
begin
Result:=False;
if pHead^.Next=pHead then
Result:=True;
end;
function TStack.GetData(var a_ch:char):boolean;
begin
if not IsEmpty then
begin
a_ch:=pHead^.Prev^.Data;
Result:=True;
end
else
begin
Result:=False;
end;
end;
function TStack.PopData(var a_ch:char):boolean;
var
pTemp
StackRec;
begin
if not IsEmpty then
begin
pTemp:=pHead^.Prev;
a_ch:=pTemp^.Data;
pHead^.Prev:=pTemp^.Prev;
pTemp^.Prev^.Next:=pHead;
FreeMem(pTemp);
Result:=True;
end
else
Result:=False;
end;
function TStack.PushData(a_ch:char):boolean;
var
pTemp
StackRec;
begin
GetMem(pTemp,SizeOf(TStackRec));
pTemp^.Data:=a_ch;
pTemp^.Next:=pHead;
pTemp^.Prev:=pHead^.Prev;
pHead^.Prev^.Next:=pTemp;
pHead^.Prev:=pTemp;
Result:=True;
end;