GStack.c++ 1.09 KB
#include <assert.h>

#include "GStack.h"


template <class T>
GStack<T>::GStack (int size)
{
    fBase = fTop = new T[size];
    fSize = size;
}


template <class T>
GStack<T>::~GStack (void)
{
    if (fBase)
	delete[] fBase;
}


template <class T> void
GStack<T>::Push (const T& item)
{
    assert (fTop - fBase < fSize);

    if (fTop - fBase < fSize)
	*fTop++ = item;
}


template <class T> Boolean
GStack<T>::Pop (T& item)
{
    if (fTop == fBase)
	return False;

    item =  *--fTop;
    return True;
}


template <class T> int
GStack<T>::GetNum (void)
{
    return fTop - fBase;
}


#ifndef NDEBUG
template <class T> Boolean
GStack<T>::Test (void)
{
    GStack<int>	intStack(3);
    int		x;

    assert (intStack.GetNum () == 0);

    intStack.Push (1);
    intStack.Push (2);
    intStack.Push (3);

    assert (intStack.GetNum () == 3);

    assert (intStack.Pop (x));
    assert (x == 3);
    assert (intStack.Pop (x));
    assert (x == 2);
    assert (intStack.Pop (x));
    assert (x == 1);
    
    assert (intStack.Pop (x) == False);
    assert (intStack.GetNum () == 0);

    return True;
}
#endif