ezstrstk.pas

来自「很经典的Delphi数据结构算法包,支持Delphi 1.0~ 7.0 和 De」· PAS 代码 · 共 99 行

PAS
99
字号
unit EZStrStk;
  {-Example unit defining a stack for strings}

  {Note: the raison d'etre of this object definition is to show how
         easy it is to define an object based on one of EZDSL classes.
         And one that you don't have to continually remember to
         typecast the objects when you're adding or removing them from
         the container; all the nastiness is hidden!}

{$I EzdslDef.inc}
{---Place any compiler options you require here----------------------}


{--------------------------------------------------------------------}
{$I EzdslOpt.inc}

interface

uses
  SysUtils,
  EzdslSup,
  EzdslStk;

type
  {A stack for storing strings}
  TStringStack = class
    private
      Stack : TStack;

    public
      constructor Create;
        {-Initialise the stack}
      destructor Destroy; override;
        {-Destroy the stack}

      function Count : longint;
        {-Return the number of strings in the stack}
      function IsEmpty : boolean;
        {-Return true if the stack is empty}
      function Pop : string;
        {-Return the string on the top of the stack after popping it}
      procedure Push(const S : string);
        {-Return true if the string was pushed onto the stack}
  end;

implementation


{===TStringStack implementation======================================}
constructor TStringStack.Create;
begin
  Stack := TStack.Create(true);
  Stack.DisposeData := EZStrDisposeData;
end;
{--------}
destructor TStringStack.Destroy;
begin
  Stack.Free;
end;
{--------}
function TStringStack.Count : longint;
begin
  Count := Stack.Count;
end;
{--------}
function TStringStack.IsEmpty : boolean;
begin
  IsEmpty := Stack.IsEmpty;
end;
{--------}
function TStringStack.Pop : string;
var
  PS : PEZString;
begin
  Result := '';
  if not IsEmpty then begin
    PS := PEZString(Stack.Pop);
    if Assigned(PS) then begin
      Result := PS^;
      Stack.DisposeData(PS);
    end;
  end;
end;
{--------}
procedure TStringStack.Push(const S : string);
var
  PS : PEZString;
begin
  PS := EZStrNew(S);
  try
    Stack.Push(PS);
  except
    Stack.DisposeData(PS);
    raise
  end; {try..except}
end;
{====================================================================}

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?