Question: STEP BY STEP CODE TRACE PLEASE BE DETAILED public class ReverseStack { public static void main(String args[]) { Stack st = new Stack (); st.push(1);

STEP BY STEP CODE TRACE PLEASE BE DETAILED

public class ReverseStack {

public static void main(String args[]) {

Stack st = new Stack();

st.push(1);

st.push(2);

st.push(3);

st.push(4);

st.push(5);

System.out.println("Original Stack: " + st + " ");

reverse(st);

System.out.println("Reversed Stack: " + st);

}

public static void reverse(Stack stack){

if(!stack.isEmpty()){

int x = stack.pop();

reverse(stack);

insert_at_bottom(stack, x);

/* int x = 5

* int x = 4

* int x = 3

* int x = 2

* int x = 1

*

*

*/

}

}

public static void insert_at_bottom(Stack stack, int x){

if(stack.isEmpty()){

stack.push(x);

return;

}

int temp = stack.pop();

insert_at_bottom(stack, x);

stack.push(temp);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!