Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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?. image text in transcribed
image text in transcribed
image text in transcribed
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

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

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions