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.
static int rec_call(Dllist d, Dllist tmp, int cs)
{
if (tmp == d) return cs; else return rec_call(d, tmp->flink, cs+1);
}
int dlls(Dllist d)
{
return rec_call(d, d->flink, 0);
}
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.