General Stack Body
MC logo
 

General Stack Body

Ada Code Examples


<<General Stack Package Download General Stack Client>>
--
-- This is the body of the integer stack package.  This file is named
-- int_stack.adb.
--
package body G_Stack is
   -- Push an integer.  If the stack is already full, ignore the push.
   procedure Push(S: in out Gen_Stack; I: Item_Type) is
   begin
      if S.Size < Max_Size then
         S.Size := S.Size + 1;
         S.Data(S.Size) := I;
      else
         raise Stack_Overflow;
      end if;
   end Push;

   -- Pop and integer.  If the stack is empty, a zeor is placed in I.
   procedure Pop(S: in out Gen_Stack; I: out Item_Type) is
   begin
      if S.Size > 0 then
         I := S.Data(S.Size);
         S.Size := S.Size - 1;
      else
         raise Stack_Underflow;
      end if;
   end Pop;

   -- Get the top item, or 0 if the stack is empty.
   function Top(S: Gen_Stack) return Item_Type is
   begin
      if S.Size > 0 then
         return S.Data(S.Size);
      else
         raise Stack_Underflow;
      end if;
   end Top;

   -- Tell if the stack is empty.
   function Empty(S: Gen_Stack) return Boolean is
   begin
      return S.Size = 0;
   end Empty;

   -- Tell if the stack is empty.
   function Full(S: Gen_Stack) return Boolean is
   begin
      return S.Size = Max_Size;
   end Full;

   -- Make the stack empty.
   procedure Clean(S: in out Gen_Stack) is
   begin
      S.Size := 0;
   end Clean;
end G_Stack;
<<General Stack Package General Stack Client>>