General Stack Package
MC logo

General Stack Package

Ada Code Examples

<<Linked List of Strings Download General Stack Body>>
-- This is a generalized stack implemented using a generic package.  The
-- type and size of the stack are parameters of the generic.
   -- These are parameters to the package, including a type.
   Max_Size: Positive;          -- The maximum size of the stack.
   type Item_Type is private;   -- The type of items in the stack.  This must
                                -- be definite, and the private means that this
                                -- package may not examine its internals.
package G_Stack is
   -- Here is the stack type itself.
   type Gen_Stack is private;

   -- Here are the exceptions.
   Stack_Underflow, Stack_Overflow: exception;

   -- All the public stack operations.
   procedure Push(S: in out Gen_Stack; I: Item_Type);
   procedure Pop(S: in out Gen_Stack; I: out Item_Type);
   function Top(S: Gen_Stack) return Item_Type;
   function Empty(S: Gen_Stack) return Boolean;
   function Full(S: Gen_Stack) return Boolean;
   procedure Clean(S: in out Gen_Stack);

      -- The items here cannot be accessed by clients of the package.
      type Stack_Data_Type is array(1..Max_Size) of Item_Type;
      type Gen_Stack is record
         Size: Integer := 0;
         Data: Stack_Data_Type;
      end record;
end G_Stack;
<<Linked List of Strings General Stack Body>>