Reverse a Stack using Recursion

Write a Code to reverse a stack using recursion.

Answer:


template <typename T>
void ReverseStack(Stack s)
{
if ( s.isEmpty() )
return; // nothing to do
else
{
T elem = s.Top();
s.Pop();
ReverseStack(s);
s.Push(elem);
}
}

No comments: