Write a method removeMin that takes a stack of
integers as a parameter and that removes and returns the smallest value from
the Stack. For example, if a variable called s stores the following
sequence of values:
bottom [2, 8, 3, 19, 7, 3, 2, 42, 9, 3, 2, 7, 12, -8, 4] top
and we make the following call:
int n = removeMin(s);
the method removes and returns the value -8 from the stack, so that the
variable n will be -8 after the call and s will store the following values:
bottom [2, 8, 3, 19, 7, 3, 2, 42, 9, 3, 2, 7, 12, 4] top
If the minimum value appears more than once, all occurrences of the minimum
should be removed from the stack. For example, given the ending value of
the stack above, if we again call removeMin(s), the method would return 2
and would leave the stack in the following state:
bottom [8, 3, 19, 7, 3, 42, 9, 3, 7, 12, 4] top
You are to use one queue as auxiliary storage to solve this problem. You
may not use any other auxiliary data structures to solve this problem,
although you can have as many simple variables as you like. You also may
not solve the problem recursively. Your solution must run in O(n) time.
Your method should throw an IllegalArgumentException if the stack is empty.
In writing your method, assume that you are using the Stack and Queue
interfaces and the ArrayStack and LinkedQueue implementations discussed in
lecture.