In: Computer Science
char insert_at_bottom(Stack *stack1, int x) {
if(stack1.size() == 0)
stack1.push(x);
else {
// All items are held in Stack until
we reach end of the stack When the stack becomes empty, the
stack1.size() becomes 0, the above if part is executed and the item
is inserted at the bottom.
int a = stack1.top();
stack1.pop();
insert_at_bottom(x);
// push all the items held in current
Stack once the item is inserted at the bottom.
stack1.push(a);
}
}
char reverse(Stack *stack1) {
if(stack1.size()>0) {
// Hold all items in Stack until we
have reached the end of the stack.
int x = stack1.top();
stack1.pop();
reverse();
// Insert all the items held in Stack
one by one from the bottom to top.
insert_at_bottom(x);
}
}
The above two functions can help in reversing the stack. Reverse() is the recursive function that we need.
Hope this helps. If you have any queries or suggestions regarding the answers please leave them in the comments section so I can update and improve the answer. Thank you.