Integer Stack with Exceptions Body
MC logo
 

Integer Stack with Exceptions Body

Ada Code Examples


<<Integer Stack with Exceptions Download Integer Stack with Exceptions Client>>
--
-- This is the body of the integer stack package.  This file is named
-- int_stack.adb.
--
package body I_Stack2 is
   -- Push an integer.  If the stack is already full, raise Stack_Overflow.
   procedure Push(S: in out Int_Stack; I: Integer) 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, raise Stack_Underflow.
   procedure Pop(S: in out Int_Stack; I: out Integer) 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.  If the stack is empty, raise Stack_Underflow.
   function Top(S: Int_Stack) return Integer 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: Int_Stack) return Boolean is
   begin
      return S.Size = 0;
   end Empty;

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

   -- Make the stack empty.
   procedure Clean(S: in out Int_Stack) is
   begin
      S.Size := 0;
   end Clean;
end I_Stack2;
<<Integer Stack with Exceptions Integer Stack with Exceptions Client>>