home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programming Unleashed / Delphi_Programming_Unleashed_SAMS_Publishing_1995.iso / units / stacks.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-03-21  |  711 b   |  48 lines

  1. unit Stacks;
  2. { A simple exercise for the stacks unit }
  3.  
  4. interface
  5. uses
  6.   Classes;
  7.   
  8. type
  9.   TStack = class(TStringList)
  10.     constructor Create;
  11.     procedure Push(S: String);
  12.     function Pop: String;
  13.     function IsEmpty: Boolean;
  14.   private
  15.     First, Last: LongInt;
  16.   end;
  17.  
  18. implementation
  19.  
  20. constructor TStack.Create;
  21. begin
  22.   inherited Create;
  23.   First := 0;
  24.   Last := 0;
  25. end;
  26.  
  27. procedure TStack.Push(S: String);
  28. begin
  29.   Last := Add(S);
  30. end;
  31.  
  32. function TStack.Pop: String;
  33. var
  34.   S: String;
  35. begin
  36.   S := Strings[First];
  37.   inc(First);
  38.   Pop := S;
  39. end;
  40.  
  41. function TStack.IsEmpty: Boolean;
  42. begin
  43.   if First = Last then IsEmpty := True
  44.   else IsEmpty := False;
  45. end;
  46.  
  47. end.
  48.