home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / ada_1 / Examples_ada_stack < prev    next >
Encoding:
Text File  |  1994-08-14  |  1.1 KB  |  47 lines

  1. -- ++
  2. -- A simple stack for integers.
  3. -- --
  4.  
  5. package body Stack is
  6.  
  7. procedure Push ( The_Stack : in out Stack; Value : in Integer ) is
  8. begin
  9.    if The_Stack.Top = The_Stack.Max then
  10.       raise Overflow;
  11.    end if;
  12.    The_Stack.Top := The_Stack.Top + 1;
  13.    The_Stack.S ( The_Stack.Top ) := Value;
  14. end Push;
  15.  
  16. procedure Pop  ( The_Stack : in out Stack; Value : out Integer ) is
  17. begin
  18.    if The_Stack.Top = 0 then
  19.       raise Underflow;
  20.    end if;
  21.    Value := The_Stack.S ( The_Stack.Top );
  22.    The_Stack.Top := The_Stack.Top - 1;
  23. end Pop;
  24.  
  25. function  "="  ( The_Stack : in Stack; A_Stack : in Stack ) return Boolean is
  26. begin
  27.    if The_Stack.Top /= A_Stack.Top then
  28.       return False;
  29.    elsif The_Stack.Max < A_Stack.Top or else A_Stack.Max < The_Stack.Top then
  30.       return False;
  31.    else
  32.       return The_Stack.S ( 1 .. The_Stack.Top ) = A_Stack.S ( 1 .. A_Stack.Top );
  33.    end if;
  34.    return True;
  35. end "=";
  36.  
  37. function Empty ( The_Stack : in Stack ) return Boolean is
  38. begin
  39.    return The_Stack.Top = 0;
  40. end Empty;
  41.    
  42. function Full  ( The_Stack : in Stack ) return Boolean is
  43. begin
  44.    return The_Stack.Top = The_Stack.Max;
  45. end Full;
  46.  
  47. end Stack;