How to write the recurrence relation and find the complexity of this algorithm?
Reverse stack using 1 extra stack
hey @f2016115, the approch is to pop each element one by one and when you pop one element from stack1, transfer all other elements of stack1 to stack2 and than push poped element to stack1. Finally transfer element back to stack1 from stack2.
There is no need of recursion when you are reversing using 1 extra stack, Just maintain a separate function for transferring the element from one stack to another.
Consider this
for(i=0 to n)
{
pop topmost element from stack1.
transfer N-i-1 elements from Stack1 to stack2 //N=5,i=0,N-i-1=4…transferring 4 elements
push the earlier popped element in stack1.
transfer those N-i-1 elements back to Stack1 from stack2
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.