Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA programming In this exercise, in addition to the pop() and push() methods, implement a stack with a max() method, which finds the maximum element

JAVA programming

In this exercise, in addition to the pop() and push() methods, implement a stack with a max() method, which finds the maximum element in the stack. Assume that underflow and overflow errors will not occur in this exercise.

Input: A number representing the number of operations to the stack and a string of operations on the stack.

Output: For each max operation, print the current maximum value in the stack. If the stack is empty when max is queried, then print "-100" (excluding the quotation marks). Only the max operation prints an output.

Sample Cases:

Example 1:

Input: 5 //Number of stack operations

push 2 //an operation that pushes the element '2' to the stack

push 1

max //an operation for getting the current max in the stack

pop //an operation for popping the top element of the stack

max

Output: 2 // output of first max operation

2 // output of second max operation

=======================================================

Example 2:

Input: 5

push 1

push 2

max

pop

max

Output: 2 // output of first max operation

1 // output of second max operation

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_2

Step: 3

blur-text-image_3

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

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

Define binary search and explain when to use it.

Answered: 1 week ago