Ada : Generic stacks (ii)

In the previous post we looked at the structure of a generic stack in Ada, stored in and gstack.adb. Now let’s look at how it can be used to create stacks of various types. Consider the program below, which creates and uses two stacks: one for characters and one for integers.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with gstack; 

procedure stackG is
  package stackC is new gstack(100,character);
  use stackC;
  a,b : character;
  package stackI is new gstack(100,integer);
  use stackI;
  i,j : integer;

end stackG;

The first stack created is named stackC, and is a character stack with 100 elements. It is created by accessing the generic stack using the with keyword. Then a new instance of the stack is created using new. Similarly, stackI is created to stored 100 integers. Easy right? Now in the body of the program, pop() and push() are used to store and retrieve, first a character, then an integer.

Note that there is no need to specify which stack is being used. Although if you feel uneasy about this, you could also useĀ stackC.push(a) in place of push(a).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s