A stack is a technique of creating a list so that the
last item added to the list is also the first one that would be removed. It
can be illustrated as placing a few cups in a box that can receive only one
on top of another (no adjacent cup). When it comes time to get one of those
cups, you must first access the top one; that is, the last one that was
added. In other words, at one time, the items in the list appear in the
reverse order they were added. This technique of building a list is referred
to as first-in last-out (FILO).

To create a collection class that supports stack
functionality, you have various options between arrays and linked lists.

Introduction to Creating a Stack Using an Array

To perform stack operations using a collection,
you can create a class whose main member is an array. Here is an
example of starting such a class:

(We are starting from an interface but you can
omit it if you want.) Here is an example:

public class Stack<T>
{
private T[] items;
}

We are also creating the class as a generic one but you
can remove <T> then use a data type of your choice wherever T is used as a
type. Here is an example:

public class Stack
{
double[] items;
}

)

Of course, whenever you use an array, you should (must)
make sure you initialize it prior to its being used. You can initialize the
array in the body of the class or you can use a constructor. Here is an
example:

As always, as a courtesy, you should make sure you keep
a count of the items in a collection class. This is usually done by using a
read-only property that returns the size of the array. This can be done as
follows:

Using this mechanism, you can create a property that
holds the information about the stack being empty or not. When implementing
this method, you can check the value of the size. If it is 0, the list is
empty. If the size is greater than 0, the list is not empty. Here is an
example of defining this method:

To add an item to a stack, if the list is empty, which
means its size is 0, you simply position the first item. If the stack
already contains at leas one item, you must push the existing item down (or
in the moving direction) to create an empty space for the new item, then
position the new item in the new empty position. For this reason, this
operation is referred to as pushing.

If you are using an array, to push the new item, from
the end of the list, first move each item one position down to make the 0
index available. Then, assign the new item to that index. If your class is
keeping a count of the items in its list, after adding the new item, you
should increase the number of items. Here is an example of performing this
operation:

The process of getting an item of a stack is referred to
as peeking. Remember that, in a stack, you have access to only one item, the
last one to be added. Therefore, to peek an item, create a method that
simply returns the item in the last index. This would be the item at the
size - 1. Here is an example of creating and using a peeking method:

The processing of removing an item from a stack is
referred to as poping it. Once again, remember that you only have access to
the top item, which is the last item that was added. If you are using an
array, to pop an item, in your method:

If you want, you can provide support foreach
item of the collection. To do this, you would create a method named
GetEnumerator that returns an IEnumerator object.
When implementing this method, start from the end of the list, use a
while loop to yield return the current item and decrement the count
each time. Here is an example:

The operations we have implemented so far are typical of
a stack. One of the routine operations you can perform consists of checking
the availability of an item. To perform this operation, create a method (you
can call it Contains) that is passed the item to look for. When implementing
the method, you can use a loop to visit each item. If you find an item that
matches the argument, return true. If you get to the end of the list and
there was no item that matched the argument, let the method return false.
Here is an example of how this can be done: