Answer to Question 27A-4

Here is a definition of destroy.

  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. As a result, mistakes are more likely.

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