33D. Example: Modify the Items in a Linked List

Suppose that you want doubleAll(L) to modify the items in list L by doubling each. For example, if L is [1, 3, 5], then after doing doubleAll(L), L will contain [2, 6, 10]. The cases are simple.

  1. If L is empty there is nothing to do.

  2. if L is not empty then double its head and call doubleAll on its tail.

Notice that this function does not need to change the pointer stored in L. It only changes what is stored in the list cells. so we do not use call by reference.
  void doubleAll(List L)
  {
    if(L != NULL)
    {
      L->head = 2 * L->head;
      doubleAll(L->tail);
    }
  }


Exercises

  1. Is the definition of doubleAll tail-recursive? Answer