I have to implement this function such that it returns the nth item from the stack without using a helper stack (or any other data structure) and still have the stack in its original state in the end, using recursion?
I have managed to implement it but my solution does not leave the stack in its original state.
template <class Type>
Type getNth( stack<Type> & s, int n)
{
if(n == 1)
{
return s.top();
}
else
{
s.pop();
return getNth(s, n - 1);
}
}