{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Str &amp; Algorithm HW Solutions 29

# Data Str &amp; Algorithm HW Solutions 29 - long...

This preview shows page 1. Sign up to view the full content.

29 } 4.15 FIBobj stores a value and an operand type. If the operand is IN , then the value is a parameter to the Fibonacci function. If the operand is OUT , then the value is an intermediate result. When we pop of an IN value, it must be evaluated. When we have available two intermediate results, they can be added together and returned to the stack. enum FIBOP {IN, OUT}; class FIBobj { public: int val; FIBOP op; FIBobj(int v, FIBOP o) { val = v; op = o; }
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: }; long fibs(int n) { AStack<Fibobj> S; FIBobj f; f.val = n; f.op = IN; S.push(f); while (S.length() > 0) { S.pop(f); int val = f.val; FIBOP op = f.op; if (op == IN) if (val <= 2) { f.val = 1; f.op = OUT; S.push(f); } else { f.val = val - 1; f.op = IN; S.push(f); f.val = val - 2; S.push(f); } else // op == OUT if (S.length() > 0) { // Else do nothing, loop ends S.pop(f); // 2nd operand if (f.op == OUT) {...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online