-2
public static int min2(Stack <Integer> st, int min) {
        if(st.isEmpty()) {
            return min;
        }
        int num= st.pop();
        if(num<min) {
            min=num;
        }
        return min2(st,min);

I wrote this code for finding min value in stack and I used another parameter->int min which I'd like to get rid of & don't understand what I should ask from the user to enter to the parameter.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

Your code can be brought down to this iterative approach:

public static min3(Stack<Integer> st) {
    int min = Integer.MAX_VALUE;
    while(!stack.isEmpty()) {
        int current = stack.pop();
        if(current < min)
            min = current;
    }
    return min;
}

This gets rid of the parameter, but if you want to keep it recursive, you have to somehow store the current minimum. Either, passed as a parameter, or stored in a static variable.

Also: Because parameter-objects get passed as references, you will destroy your stack in the process.

xtay2
  • 453
  • 3
  • 14
  • "Because Java is pass-by-reference" Java is **always** pass-by-value. From linked answer: "*Pass-by-value means that the value of a variable is passed to a function/method. Pass-by-reference means that a reference to that variable is passed to the function. The latter gives the function a way to change the contents of the variable.*". Because Java is always pass-by-value it makes it impossible to write method like `swap(String x, String y){...}` which after calling `swap(str1, str2);` would be able to assign *local variable* `str1` value from *local variable* `str2` and vice versa. – Pshemo Oct 16 '22 at 22:40
  • To be more precise, it is impossible to write `swap(x, y)` for *any* type, not only for String. Anyway to understand that "Java pass the reference by value" we may need to understand [What is the difference between a variable, object, and reference?](https://stackoverflow.com/q/32010172) – Pshemo Oct 16 '22 at 23:05
  • What I meant to say is, that if no new stack/deepcopy gets passed, the used one will get modified. Parameters are reference types, not values. – xtay2 Oct 17 '22 at 08:58
0

Recursion deals with a partial result in every call, and an extra termination condition. This will result in at least one extra parameter in general.

Here the parameter is the minium of the popped elements.

public static int min(Stack <Integer> st) {
    return minRecursively(st, Integer.MAX_VALUE);
}
private static int minRecursively(Stack <Integer> st, int min) {
    if (st.isEmpty()) {
        return min;
    }
    int num = st.pop();
    if (num < min) {
        min = num;
    }
    return minRecursively(st, min);
}

Your implementation emptied the entire stack. Better would be:

private static int minRecursively(Stack <Integer> st, int min) {
    if (st.isEmpty()) {
        return min;
    }
    int num = st.pop();
    if (num < min) {
        min = num;
    }
    min = minRecursively(st, min);
    stack.push(num); // Leave the stack unchanged.
    return min;
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138