CS360 Final -- December 11, 1999. Question 3

You've just been hired to take the place of ``Hank the hacker,'' who was fired because was too cavalier of a programmer and human being for your conservative-minded company. As your first job, you've been handed a piece of Hank's code. Here it is:
jmp_buf Env;
int CS;
Dllist Tmp;
Dllist D;

static rec_call()
{
  if (Tmp == D) longjmp(Env, 1);
  Tmp = Tmp->flink;
  CS++;
  rec_call();
}

int dlls(Dllist d)
{
  if (setjmp(Env) == 1) return CS;
  CS = 0;
  D = d;
  Tmp = d->flink;
  rec_call();
}
Part 1: It is obvious that Hank deserved to be fired. Rewrite this code with no global variables, no recursion, and no use of setjmp/longjmp.

Part 2: When Hank was confronted with this code, his explanation was the following: ``I inherited this code from this functional programming loser. All I did was make it more efficient.'' Here is the code that Hank inherited.

Hank is right. He did make the code more efficient in two distinct ways. One involves the global variables, and one involves setjmp/longjmp. Explain the two ways in which Hank's code is more efficient than his predecessor's code.

Part 3: Your code, Hank's code, and his predecessor's code all have the same computational (big-O) complexity with respect to the size of the list d. If d has n elements, what is the computational complexity of all of your codes?

Part 4: Explain why your code is even more efficient than Hank's.