reverse of stack

reverse the order of elements on stack S using one additional stack and some additional nonarray variables

you can pop from one stack and push popped element into second stack. You can get reverse of a stack.

stackP1,P2;
for(int i=0;i<10;i++)
P1.push(i);
while(!P1.empty())
{
int p= P1.top();
P2.push§;
P1.pop();
}
P1= P2;
Now stack P1

this is a nice explanation of doing that using recursion

we have to use only additional one stack

yes you can do that by using 1 additional stack ,

//P1 is the stack reverse you want.
//P2 is the reverse of a stack.
while(!P1.empty())
{
int p= P1.pop()
P2.push§;
}

i want reverse of elements in the p1 itself.

and what about using non array variables while reversing the stack with one additional stack?

here non array variable is variable p

thanks! :slight_smile:

using recursion how?

it is explained there in the link i have given