Stack Class example lazarus / 8-Other ---------------------------------------------------------------------- BenJones · 2011-08-20 06:47 Hi this is one of my first class examples in lazarus it a simple stack, it uses variants so you can add strings, numbers, well hope you find some use for it comments are welcome cos I am sure I can do things better on it so ant mistakes please point them out, so I can inprove on it. here is the class code: Code: [Select]unit cStack; {$mode objfpc}{$H+} interface uses   Classes, SysUtils; type   TCompareMode = (Text, Binary); type   TMyStack = class   private     //Hold stack size and stack position.     Stack_size, StackPtr: integer;     //This hold items for our stack     //Stack items     T_Items: array of variant;     //Compare mode.     CmpMode: TCompareMode;     //Set compare mode.     procedure SetCompare(Value: TCompareMode);     //get compare mode.     function GetCompare(): TCompareMode;   public     //To create the stack     constructor Create(Size: integer);     //Return stack count     property Count: integer read Stack_size;     property Compare: TCompareMode read GetCompare write SetCompare;     //Push items onto stack     procedure Push(Item: variant);     //Pop items of stack     function Pop(): variant;     //Peek at the first item.     function Peek(): variant;     //Tells us if stack id full.     function IsFull(): boolean;     //Tells us if stack empry.     function IsEmpty(): boolean;     //Check for item in stack.     function Contains(Item: variant): boolean;     procedure Free;   end; implementation constructor TMyStack.Create(Size: integer); begin   StackPtr := 0;   Stack_size := Size;   //Set size of stack   SetLength(T_Items, Stack_size); end; function TMyStack.GetCompare(): TCompareMode; begin   //Set compare mode.   Result := CmpMode; end; procedure TMyStack.SetCompare(Value: TCompareMode); begin   //Get compare mode.   CmpMode := Value; end; procedure TMyStack.Push(Item: variant); begin   if not IsFull then   begin     //Push items on the stack.     T_Items[StackPtr + 1] := Item;     StackPtr := StackPtr + 1;   end; end; function TMyStack.Pop(): variant; begin   if not IsEmpty then   begin     //Return item from stack.     Result := T_Items[StackPtr];     StackPtr := StackPtr - 1;   end; end; function TMyStack.Peek(): variant; begin   Peek := T_Items[StackPtr + 1]; end; function TMyStack.IsFull(): boolean; begin   IsFull := (StackPtr = Stack_size); end; function TMyStack.IsEmpty(): boolean; begin   Result := (StackPtr = 0); end; function TMyStack.Contains(Item: variant): boolean; var   X: integer;   Found: boolean;   sItem1, sItem2: variant; begin   Found := False;   //Look for item in stack.   for X := low(T_Items) to high(T_Items) do   begin     //Compare mode for text.     if (cmpMode = TCompareMode.Text) then     begin       sItem1 := LowerCase(T_Items[x]);       sItem2 := LowerCase(Item);     end;     //Compare mode for binary.     if (cmpMode = TcompareMode.Binary) then     begin       sItem1 := T_Items[X];       sItem2 := Item;     end;     //Check for item in stack.     if (sItem1 = sItem2) then     begin       Found := True;       break;     end;   end;   Contains := Found; end; procedure TMyStack.Free(); begin   Stack_size := 0;   StackPtr := 0;   SetLength(T_Items, 0); end; end. This is a small example using the class put code into command button. Code: [Select]var   MyStack: TMyStack; begin   //Create stack object   MyStack := TMyStack.Create(5);   //Set compare mode.   MyStack.Compare := MyStack.Compare.Text;   //Add some digits   MyStack.Push(1);   MyStack.Push(2);   MyStack.Push(3);   //Display results   ShowMessage(MyStack.Pop());   ShowMessage(MyStack.Pop());   ShowMessage(MyStack.Pop());   //Add some strings   MyStack.Push('A');   MyStack.Push('B');   MyStack.Push('C');   //Check for item A in stack   ShowMessage('A in stack = ' + BoolToStr(MyStack.Contains('a')));   //Display results   ShowMessage(MyStack.Pop());   ShowMessage(MyStack.Pop());   ShowMessage(MyStack.Pop());   //Show size of stack   ShowMessage('Stack size: ' + IntToStr(MyStack.Count));   //Free up space.   MyStack.Free; ---------------------------------------------------------------------- bertherngreen · 2011-08-20 08:54 Hi, Don't want to be rude, but maybe fpc could save a lot of time for you: It allready implements a TStack, TQueue and more  http://www.freepascal.org/docs-html/fcl/contnrs/tstack.html Kind regards, Bert ---------------------------------------------------------------------- Leledumbo · 2011-08-20 16:54 QuoteDon't want to be rude, but maybe fpc could save a lot of time for you: It allready implements a TStack, TQueue and moreThey're pointer based, which makes usage a little more difficult. There're also TObject based one, but needs casting when retrieving. The best for me is still the generics style from fcl-stl (included in fpc trunk). ---------------------------------------------------------------------- BenJones · 2013-06-15 18:24 Just thought I do an update of my stack class see above codes. New features include. Peek Contains CompareMode Stack IsFull Stack IsEmpty Free