listsstacksand queues /*print container from start up to but not including end *template void printiterator startiterator endostream out cout whiletrue ifstart =end returnout <*start+<endl/print and advance start figure printing container without recursiona compiler might do this (you should notcontains , elements to printthere will be stack of , activation records representing the nested calls of line activation records are typically large because of all the information they containso this program is likely to run out of stack space (if , elements are not enough to make the program crashreplace the number with larger one this program is an example of an extremely bad use of recursion known as tail recursion tail recursion refers to recursive call at the last line tail recursion can be mechanically eliminated by enclosing the body in while loop and replacing the recursive call with one assignment per function argument this simulates the recursive call because nothing needs to be savedafter the recursive call finishesthere is really no need to know the saved values because of thiswe can just go to the top of the function with the values that would have been used in recursive call the function in figure shows the mechanically improved version generated by this algorithm removal of tail recursion is so simple that some compilers do it automatically even soit is best not to find out that yours does not recursion can always be completely removed (compilers do so in converting to assembly language)but doing so can be quite tedious the general strategy requires using stack and is worthwhile only if you can manage to put the bare minimum on the stack we will not dwell on this furtherexcept to point out that although nonrecursive programs are certainly generally faster than equivalent recursive programsthe speed advantage rarely justifies the lack of clarity that results from removing the recursion the queue adt like stacksqueues are lists with queuehoweverinsertion is done at one end whereas deletion is performed at the other end