Name: ________________________________

Honor Code Acknowledgment: _____________________________


Random Quiz 5

CPS 100, Fall 1996

6 points

Due: October 29


Problem 1: It's A Mystery To Me (3 pts)

A doubly linked list is represented using the following Node definition (Assume the "ends" of the list are set to NULL, that is, the next field of the rightmost node in the list is NULL and the prev field of the leftmost node in the list is NULL).

struct Node
{
   int data;
   Node * next;
   Node * prev;
};

int Mystery(Node * list)
{
    if (list == NULL)
      return 0;
    else
       return 1 + Mystery(list->next) + Mystery(list->prev);
}

  1. What happens when Mystery(list) is called and list points to a one node list?
  2. What happens when Mystery(list) is called and list points to the leftmost node of a 7 node list?
  3. What happens when Mystery(list) is called and list points to the middle node of a 7 node list?

Problem 2: Still a Mystery (3 points)

int Mystery2(int n)
{
    if (n <= 1)
      return 0;
    else 
       return 1 + Mystery2(n/2) + Mystery2(n/4);
}

  1. Draw the tree of recursive calls for Mystery2(9).
  2. What is the return value of Mystery2(9)?