Spaghetti code
Donald Knuth had a counter argument
to Dijkstra’s paper called "Goto considered harmful". His contention was
that there are some algorithms better written with gotos. Ont Below
is a C program that implements an in-order traversal of a binary tree.
Study it and then carry out the following transformations. For each step,
you must explain what you are doing to get full credit. Merely writing
code is not good enough.
Draw a flow chart for the program.
Remove all the gotos leaving only structured
programming constructs.
Replace all loops with recursive calls
and eliminate the stack ‘stack’.
Replace all tail-recursive calls with
loops, without using an explicit stack.
Due date
Oct. 7th. By 5:00 pm.
int main(int argc, char*
argv[]) {
const int empty
= 0, nil = -1, root = 0;
int stack[100],
top = 0;
int A[] = {0, 1,
2, 3, 4, 5, 6};
int L[] = {1, 3,
5, nil,nil, nil, nil};
int R[] = {2, 4,
6, nil, nil, nil, nil};
int t = root;
l1:
if (t == nil) goto
l5;
stack[top++] = t;
t = L[t];
goto l1;
l2:
t = stack[--top];
printf("%d\n", A[t]);
t = R[t];
goto l1;
l5:
if (top != empty)
goto l2;
}