Question: Need help with this code, I need to get the max value of a stack. I create another stack object that is supposed to hold

Need help with this code, I need to get the max value of a stack. I create another stack object that is supposed to hold the max value but as you can see the output that I have is different from the one I should have. What am I doing wrong?.  Need help with this code, I need to get the max
value of a stack. I create another stack object that is supposed
to hold the max value but as you can see the output

In this part of the assignment, you will implement an ADT called MaxStack. It must implement the following API (note that we are not defining the API as an interface here): public class MaxStack> MaxStack ) constructor add x to the top of the stack remove and return the top element return the maximum value in the stack void push (T x) T pop () T getMax ) In other words, it's similar to a regular stack, but you can always quickly find the maximum element. Here is a sample client you can use for testing: public static void main(Stringl] args) ( MaxStack ms new MaxStack(); ms.push( 2) Stdout.printIntms.getMax()); // should print 2 ms.push( 3); Stdout.printin(ms.getMax()); I1 should print 3 ms.push(1); stdout.printIn(ms.getMax(;/should print 3 ms.push(3); Stdout.print Intms.getMax( / should print 3 ms.pop() Stdout.printIn(ms.getMax()) 11 should print 3 ms pop(); Stdout.println(ms.getMax()); 11 should print 3 ms.pop); Stdout.println(ms.getMax()); // should print 2 You cannot implement getMax () by searching the stack looking for the largest element. The maximum element must be readily available without searching the stack. You can assume that the Maxstack instance has at least one item any time pop () or getMax () is called. Also, getMax () does not change the stack contents

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!