previous | start | next

Stack (cont)

A Stack is NOT an Array. The operations allowed on a Array are not allowed on a Stack.

Why do this? Because it makes it easy to implement the new Stack class. For example,

template <typename T>
Stack<T>::Stack() {}

template <typename T>
bool Stack<T>::empty() const
{
  return a.size() == 0;
}

template <typename T>
void Stack<T>::push(int x) 
{
  a.add(x);
}

template <typename T>
int Stack<T>::pop()
{
  int n;
  assert(a.size() != 0 );

  n = a.remove(a.size() - 1);

  return n;
}


previous | start | next