2.10 Abstract (ADT) Stack

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

Here is the second in the series of examples implementing push-down stacks.

The package illustrated here is an example of what is called an abstract data type (ADT) package in [Booch87], [Cohen96] and [Feldman96] and is called a "type manager" in [Shumate89]. Clients of this package can declare any number of stacks that are similar to the one created in the previous example. In this case two stacks, S1 and S2, are declared by the test procedure.

Im2-9.gif (5421 bytes)

The visible part of the package declaration provides a partial declaration of Stack_Type (Note the words "is private" in the declaration.) and the two operations associated with it (Push and Pop) while the private part of the package declaration contains the complete declaration of Stack_Type. Thus clients can "see" the name of this private type and its associated operations, and can create object instances and call the operations -- but they cannot see the implementation details. Their only access to stack elements is via the Push and Pop operations.

Source Code Listing

-------------------- Abstract_Char_Stack ------------------
--  This package is an example of a type manager or ADT 
--  (Abstract Data Type) package. The type is available to 
--  clients, but its structure is private.
package Abstract_Char_Stack is
  type Stack_Type is private;
  procedure Push(Stack : in out Stack_Type;
                 Item  : in Character);
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character);
  type Space_Type is array(1..8) of Character;
  type Stack_Type is record
    Space : Space_Type;
    Index : Natural := 0;
  end record;
end Abstract_Char_Stack;
package body Abstract_Char_Stack is
  procedure Push(Stack : in out Stack_Type;
                  Item : in Character) is
    Stack.Index := Stack.Index + 1;
    Stack.Space(Stack.Index) := Item;
  end Push;
  procedure Pop (Stack : in out Stack_Type;
                 Char  : out Character) is
    Char := Stack.Space(Stack.Index);
    Stack.Index := Stack.Index - 1;
  end Pop;
end Abstract_Char_Stack;
--------------------- Test_ADT_Stack ----------------------
--  This procedure declares two stacks, pushes five 
--  characters on the first one, then uses a loop and both 
--  stacks to reverse the order of the five characters.
with Ada.Text_IO; use Ada.Text_IO;
with Abstract_Char_Stack; use Abstract_Char_Stack;
procedure Test_ADT_Stack is
  S1 : Stack_Type;
  S2 : Stack_Type;
  Ch : Character;
  Push(S1,'H'); Push(S1,'E');  
  Push(S1,'L'); Push(S1,'L');
  Push(S1,'O');                          -- S1 holds O,L,L,E,H
  for I in 1..5 loop
    Pop(S1, Ch);  
    Put(Ch);                             -- displays OLLEH
  end loop;                              -- S2 holds H,E,L,L,O

  Put_Line("Order is reversed");
  for I in 1..5 loop
    Pop(S2, Ch);
    Put(Ch);                             -- displays HELLO
  end loop;
end Test_ADT_Stack;

This package has a measure of reusability not present in the previous example, but it still lacks flexibility in that the size of the stacks that can be created and the type of elements handled remain fixed.

Related Topics

2.7 Importance and Uses of Packages 2.9 Simple (ADO) Stack
2.11 Generic Units

[ Back to top of pagePrev ] Next ]