Question: 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

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!