Вот, попробуйте это. Он использует Java Stack
, но процесс должен быть таким же.
public static <E> boolean se(Stack<E> s, E a) {
boolean answer;
if (!s.isEmpty()) {
// stack isn't empty so get value
E k = s.pop();
// meets criteria?
if (k.equals(a)) {
// yes so put it back on.
s.push(k);
// an return true
return true;
// doesn't meet criteria
} else {
// call again
answer = se(s, a);
// when the result is returned, the
// values in the call chain will be
// returned to the stack in the order they
// were removed.
s.push(k);
//And the answer from each previous call will be
// returned, resulting in the final answer.
return answer;
}
}
// stack empty with no success
// return false
return false;
}