Java only - Problem 1: Write a method with title line: This method is used to re
ID: 3724978 • Letter: J
Question
Java only - Problem 1: Write a method with title line:
This method is used to reverse the data in a stack. For example, if the stack begins storing A on top of B on top of C then it should end storing C on top of B on top of A.
Your method should work correctly without assuming anything about how the stack is implemented or how many items it stores. The only 2 stack methods that you may make use of are push and pop. The end of the stack can only be detected by catching an exception thrown by the pop method. You may also make use of queues which can be assumed to have methods enqueue and dequeue.
Explanation / Answer
public static void reverseStackf(Stack s) { Stack temp = new Stack(); while (true) { try { temp.push(s.pop()); } catch (Exception e) { break; } } while (true) { try { s.push(temp.pop()); } catch (Exception e) { break; } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.