{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Structures &amp; Alogs HW_Part_8

# Data Structures &amp; Alogs HW_Part_8 - 29 4.15 FIBobj...

This preview shows pages 1–3. 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 ;} }; 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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
30 Chap. 4 Lists, Stacks, and Queues f.val += val; s.push(f); } else { // switch order to evaluate 2nd operand FIBobj temp; temp.val = val; temp.op = OUT;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

Data Structures &amp; Alogs HW_Part_8 - 29 4.15 FIBobj...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online