Answer to Question 32-5

Here is a recursive definition.

  void destroy(List L)
  {
    if(!isEmpty(L))
    {
      destroy(tail(L));
      delete L;
    }
  }

A faster way to do this is to use a loop, but the definition is a little more involved since you have to be sure not to delete a cell too soon.

  void destroy(List L)
  {
    List* p = L;
    while(!isEmpty(p))
    {
      List* q = tail(p);
      delete p;
      p = q;
    }
  }