Question: Write a method called maxToTop that takes a stack of integers as a parameter and moves the largest value in the stack to the top

Write a method called maxToTop that takes a stack of integers as a parameter and moves the largest value in the stack to the top of the stack, leaving all other values in their original order. You may assume that the stack does not contain any duplicates. For example, if a stack s stores [27, 5, 42, -11, 0, 19] , the call of maxToTop(s) should change it to store [27, 5, -11, 0, 19, 42] . If the stack is empty, your method should not change it. Use one queue as auxiliary storage.

Step by Step Solution

3.30 Rating (156 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

public static void maxToTop Stack stack if stackisEmpty Laget return ... View full answer

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 Building Java Programs A Back to Basics Approach Questions!