Lec21Recursion_6per.pdf - EECS 402 Printing a List The...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
1 The University Of Michigan Andrew M. Morgan EECS402 Lecture 21 Savitch, Ch. 13 Recursion EECS 402 EECS 402 Andrew M Morgan 2 Printing a List class ListNodeClass { public: int val; ListNodeClass *next; }; class LinkedListClass { public: ListNodeClass *head; //data member LinkedListClass():head(0) {} //ctor void insertHead(int inVal); void printFwd() { ListNodeClass *tmp = head; cout << "Fwd List!" << endl; while (tmp != 0) { cout << "Val: " << tmp->val << endl; tmp = tmp->next; } } }; LinkedListClass ll; ll.insertHead(1); ll.insertHead(5); ll.insertHead(2); ll.insertHead(9); ll.printFwd(); From main(): Output: Fwd List! Val: 9 Val: 2 Val: 5 Val: 1 EECS 402 EECS 402 Andrew M Morgan 3 Printing a List Backwards How would you write a printBwd() member function? void LinkedListClass::printBwd() { int i, j; int count = 0; ListNodeClass *temp = head; cout << "Bwd print!" << endl; while (temp != 0) { count++; temp = temp->next; } for (i = 0; i < count; i++) { temp = head; for (j = 0; j < (count - 1) - i; j++) temp = temp->next; cout << "Val: " << temp->val << endl; } } This works, but it is very inefficient, and very error-prone. Leaving the "-1" out of the loop results in a seg fault, but it is not intuitive that it belongs there, etc... EECS 402 EECS 402 Andrew M Morgan 4 Intro To Recursion A recursive function is a function that calls itself either: Directly, in the function body Indirectly, by calling another function, which, in turn calls the recursive function Many iterative algorithms can be done using recursion Note: Just because something can be done recursively does not mean it is the best way to do it Recursion is best used when a problem can be continually broken down into problems that are easier to solve than the previous A recursive function MUST have a terminal condition, or else infinite recursion will occur Always provide a condition that will end the recursive calls Condition should be checked BEFORE making the recursive call Think of the call stack that is created when thinking of recursion EECS 402 EECS 402 Andrew M Morgan 5 Common Example, Factorial
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern