logo Practice-It logo

BJP3 Exercise 14.19: removeMin

Language/Type: Java Collections Stacks and Queues
Author: Robert Baxter (on 2013/04/01)

Write a method removeMin that accepts a Stack of integers as a parameter and removes and returns the smallest value from the stack. For example if a variable s stores these values:

bottom [2, 8, 3, 19, 7, 3, 2, 3, 2, 7, 12, -8, 4] top

and we make the following call:

int n = removeMin(s);

The method removes and returns -8, so n will store -8 after the call and s will store the following values:

bottom [2, 8, 3, 19, 7, 3, 2, 3, 2, 7, 12, 4] top

If the minimum value appears more than once, all occurrences of it should be removed. For example, given the stack above, if we again call removeMin(s), it would return 2 and leave the stack as follows:

bottom [8, 3, 19, 7, 3, 3, 7, 12, 4] top

You may use one queue as auxiliary storage. You may not use any other structures to solve this problem, although you can have as many primitive variables as you like. You may not solve the problem recursively.

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.