Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions

Question

Write a Python program to check an input number is prime or not.

Answered: 1 week ago

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What are the factors affecting organisation structure?

Answered: 1 week ago