Question: Write a method called retainMaxStack in a class called StackEx that receives an object st1 of type ArrayStack as parameter. The method finds the maximum

Write a method called retainMaxStack in a class called StackEx that receives an object st1 of type ArrayStack as parameter. The method finds the maximum of two consecutive elements of the stack st1 and retains the maximum in the stack and deletes the smaller one from the stack st1. Use common stack operations only such as push, pop, peek and isEmpty. You can create local objects of type ArrayStack in your method. Do not use array or any other data structure. Assume that the stack st1 is not empty and has even number of elements. Example: Before method call: top st1: 25, 20, 30, 50, 70, 60, 15, 35 After method call: top st1: 25, 50, 70, 35 M public class Stackex { public static void retainMaxStack (ArrayStack
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
