I would like comments on my implementation of a stack:
template <class T>
class Stack {
public:
void pop(T& element);
void push(const T& element);
Stack();
private:
int elementCount_;
int stackTop_;
static const int MAX_ELEMS = 10;
T elementArray_[MAX_ELEMS];
};
template <class T>
Stack<T>::Stack() :
elementCount_(0),stackTop_(-1)
{
}
template <class T>
void Stack<T>::push(const T& element)
{
if (stackTop_ == MAX_ELEMS-1){
std::cout << "Stack is full" << "\n" ;
return;
}
elementArray_[++stackTop_] = element;
elementCount_++;
}
template <class T>
void Stack<T>::pop(T& element)
{
if (stackTop_ == -1){
std::cout << "Stack is empty" << "\n";
return;
}
elementCount_--;
element = elementArray_[stackTop_--];
}
pop
, actually; i just saw that it returnedvoid
. \$\endgroup\$top()
method and makepop()
not return a value. \$\endgroup\$