package Others;
import java.util.Stack;
public class ReverseStackUsingRecursion {
private static Stack<Integer> stack = new Stack<>();
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
stack.push(i);
}
System.out.println("STACK");
for (int k = 9; k >= 0; k--) {
System.out.println(k);
}
reverseUsingRecursion(stack);
System.out.println("REVERSED STACK : ");
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
private static void reverseUsingRecursion(Stack<Integer> stack) {
if (stack.isEmpty())
{
return;
}
int temptop = stack.peek();
stack.pop();
reverseUsingRecursion(stack);
insertAtEnd(temptop);
}
private static void insertAtEnd(int temptop) {
if (stack.isEmpty()) {
stack.push(temptop);
} else {
int temp = stack.peek();
stack.pop();
insertAtEnd(temptop);
stack.push(temp);
}
}
}