home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch29_2a.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.0 KB  |  82 lines

  1.                         -- Chapter 29 - Programming exercise 2
  2. generic
  3.   type ITEM is (<>);
  4. package CharStak is
  5.  
  6. procedure Push(In_Char : in ITEM);       -- In_Char is added to the
  7.                                          -- stack if there is room.
  8.  
  9. procedure Pop(Out_Char : out ITEM);      -- Out_Char is removed from
  10.                                          -- stack and returned if a
  11.                                          -- character is on stack.
  12.                                          -- else a blank is returned
  13.  
  14. function Is_Empty return BOOLEAN;        -- TRUE if stack is empty
  15.  
  16. function Is_Full return BOOLEAN;         -- TRUE if stack is full
  17.  
  18. function Current_Stack_Size return INTEGER;
  19.  
  20. procedure Clear_Stack;                   -- Reset the stack to empty
  21.  
  22. end CharStak;
  23.  
  24.  
  25.  
  26.  
  27.  
  28. package body CharStak is
  29.  
  30. Maximum_Size : constant := 25;
  31. Stack_List : array(1..Maximum_Size)
  32.                             of ITEM;  -- The stack itself, purposely
  33.                                       -- defined very small.
  34. Top_Of_Stack : INTEGER := 0;          -- This will always point to
  35.                                       -- the top entry on the stack.
  36.  
  37. procedure Push(In_Char : in ITEM) is
  38. begin
  39.    if not Is_Full then
  40.       Top_Of_Stack := Top_Of_Stack + 1;
  41.       Stack_List(Top_Of_Stack) := In_Char;
  42.    end if;
  43. end Push;
  44.  
  45.  
  46. procedure Pop(Out_Char : out ITEM) is
  47. begin
  48.    if Is_Empty then
  49.       null;   -- Cannot return a blank for a generic routine.
  50.    else
  51.       Out_Char := Stack_List(Top_Of_Stack);
  52.       Top_Of_Stack := Top_Of_Stack - 1;
  53.    end if;
  54. end Pop;
  55.  
  56.  
  57. function Is_Empty return BOOLEAN is
  58. begin
  59.    return Top_Of_Stack = 0;
  60. end Is_Empty;
  61.  
  62.  
  63. function Is_Full return BOOLEAN is
  64. begin
  65.    return Top_Of_Stack = Maximum_Size;
  66. end Is_Full;
  67.  
  68.  
  69. function Current_Stack_Size return INTEGER is
  70. begin
  71.    return Top_Of_Stack;
  72. end Current_Stack_Size;
  73.  
  74.  
  75. procedure Clear_Stack is
  76. begin
  77.    Top_Of_Stack := 0;
  78. end Clear_Stack;
  79.  
  80. end CharStak;
  81.  
  82.