Data Str & Algorithm HW Solutions 30

Data Str & Algorithm HW Solutions 30 - } 4.17 int...

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

View Full Document Right 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; S.push (f); S.push (temp); } } } return val; // Correct result should be in val now } 4.16 The stack-based version will be similar to the answer for problem 4.15, so I will not repeat it here. The recursive version is as follows. int recur(int n) { if (n == 1) return 1; return recur((n+1)/2) + recur(n/2) + n;
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } 4.17 int GCD1(int n, int m) { if (n < m) swap(n, m); while ((n % m) != 0) { n = n % m; swap(m, n); } return m; } int GCD2(int n, int m) { if (n < m) swap(n, m); if ((n % m) == 0) return m; return GCD2(m, n % m); } 4.18 void reverse(Queue& Q, Stack& S) { ELEM X; while (!Q.isEmpty()) { X = Q.dequeue(); S.push(X); } while (!S.isEmpty()) { X = S.pop(); Q.enqueue(X);...
View Full Document

This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online