Data Structures & Alogs HW_Part_8

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

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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;
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 10/31/2011 for the course BCN 3431 at University of Florida.

Page1 / 4

Data Structures &amp;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 Right Arrow Icon
Ask a homework question - tutors are online