Reverse a stack

void reversestack(stack &s1){

int n = s1.size() ;
stack <int> s2 ;

while(n--)
 {
	int x = s1.top();
	s1.pop();
	
	while(s1.size()){

		s2.push(s1.top());
		s1.pop();
	}

	s1.push(x);
	
	while(s2.size()){
		s1.push(s2.top());
		s2.pop();
	}
}	

}

why this code doesn’t give the desired output of reversing a stack?

hi @aggarwal.naman21
please send complete code with main function

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.