Skip to content

Commit 727769c

Browse files
committed
Added algorithm to reverse a stack using recursion
1 parent dad4c69 commit 727769c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Misc/ReverseStackUsingRecursion.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package stacks_and_queues;
1+
/* Program to reverse a Stack using Recursion*/
22

33

44
import java.util.Stack;
@@ -38,7 +38,7 @@ public static void main(String[] args) {
3838

3939
//Function Used to reverse Stack Using Recursion
4040
private static void reverseUsingRecursion(Stack<Integer> stack) {
41-
if(stack.isEmpty())
41+
if(stack.isEmpty()) // If stack is empty then return
4242
{
4343
return;
4444
}
@@ -60,7 +60,7 @@ private static void insertAtEnd(int temptop) {
6060
int temp = stack.peek(); /* All the items are stored in call stack until we reach end*/
6161
stack.pop();
6262

63-
insertAtEnd(temptop);
63+
insertAtEnd(temptop); //Recursive call
6464

6565
stack.push(temp);
6666
}

0 commit comments

Comments
 (0)