I'm trying to write a recursive function that evaluates a Queue as a mathematical expression. The queue can have any of the following characters: { '(', ')', ' ', '*'} or any single-digit number. I want to evaluate this expression and return the result. I can not use any loops or any data structures besides the q passed in as a parameter. This is what I have but I'm not quite sure why it's not working and I can't figure out how to add recursion.
public static int evaluate(Queue<Character> q) {
if (q.peek().equals("(") || q.peek().equals(")")) {
q.remove();
}
if (!(q.peek().equals(" ") || q.peek().equals("*"))) {
int i = q.poll();
if (q.poll().equals(" ")) {
int x = q.poll();
char d = (char)(i x);
q.add(d);
}
int x;
}
return 1;
}
CodePudding user response:
