Linked Stack Package Specification
MC logo

Linked Stack Package Specification

Ada Code Examples

<<Rational Number Example Client Download Linked Stack Package Body>>
-- Linked-list based stack package.
package L_Stack is
   -- This declares that Int_Stack is a type, but the contents of the type
   -- are private, and are invisible to clients of the package.
   type Int_Stack is limited private;

   -- All the public stack operations.
   procedure Push(S: in out Int_Stack; I: Integer);
   procedure Pop(S: in out Int_Stack; I: out Integer);
   function Top(S: Int_Stack) return Integer;
   function Empty(S: Int_Stack) return Boolean;
   procedure Clean(S: in out Int_Stack);
   function "=" (S, T: Int_Stack) return Boolean;

      -- The items here cannot be accessed by clients of the package.
      type Stack_Node;                          -- There is a node type.
      type Int_Stack is access all Stack_Node;  -- Stack is ptr to a list head
      type Node_Ptr is access all Stack_Node;   -- Other node pointers.
      type Stack_Node is record                 -- Nodes are like this.
         Data: Integer;                         -- Content.
         Next: Node_Ptr;                        -- Link to next node.
      end record;
end L_Stack;
-- Note: We have no direct way to initialize Stacks when declared.  We
-- depend on the behavior of Ada that pointers are always initialized to
-- nil.
<<Rational Number Example Client Linked Stack Package Body>>