Question: 1 9 . 1 4 Lab 1 1 : A general method of removing recursion Lab Exercise The focus of today s lab is to
Lab : A general method of removing recursion
Lab Exercise
The focus of todays lab is to practice removing recursion. Weve encountered this technique before, such as when implementing iterators for binary tree objects. By the end of this exercise, youll understand how to replace recursive logic with iterative approaches while preserving functionality.
Start by reviewing the downloadable programs labaccp and labbcpp which feature recursion. They are solving the same problem but b adds animation element.
Then begin looking at the lab and focus on the one function where you have to use a stack rather than recursion.
Given code
#include #include Three towers numbered Contain a stack of disk on Moving one at a time move disk from tower to tower. Never allow a large disk on top ofg a small disk. Move the disk from tower to tower three. void move int from, int to; void printstd::vector &vec; void solvetowerwithoutrecursionint from, int to int count std::vector stack; init the stack push from, to and count while stack.empty printstack; do not delete grading engine requirespop from, to and count in reverse order of pushes TODO: your code here handle count move TODO: your code here handle count reverse the order the tail call goes on the stack first so we do it lastinstead of a recursive call just push args onto a stack instead TODO: your code here instead of move in the middle move gets pushed onto the stack too we will identify it because the count will be TODO: your code here reverse the order first recursive call goes on the stack last so we do it firstinstead of a recursive call just push args onto a stack instead TODO: your code here void printstd::vector &vec for std::vector::iterator it vec.begin; it vec.end; it std::cout it itit; std::cout std::endl; a slight change is made here to more closely echo the non recursive version void solvetowerint from, int to int count if count movefrom to; return; int thirdtower fromto; solvetowerfrom thirdtower, count; movefrom to; solvetowerthirdtower, to count; int mainint argc, char argv solvetowerwithoutrecursion; return ; #define disk #define disk #define disk #define nodisk void move int from, int to std::cout "Move disk from from to to std::endl; switch to zero base from; to; static std::string stackdisknodisk,nodiskdisknodisk,nodiskdisknodisk,nodisk; int topfrom ; while stacktopfromfromnodisktopfrom; int topto ; while stacktoptotonodisktopto; std::swapstacktopfromfromstacktoptoto; std::cout std::endl std::endl std::endl std::endl; std::cout std::endl std::endl std::endl std::endl; std::cout std::endl std::endl std::endl std::endl; std::cout std::endl std::endl std::endl std::endl; std::cout std::endl std::endl std::endl std::endl; std::cout nodisk nodisk nodisk std::endl; std::cout nodisk nodisk nodisk std::endl; for int k ;k;k std::cout stackk stackk stackk std::endl; std::cout std::endl std::endl;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
