I am having trouble understanding what I need to check for in the condition evaluation of the following while loop, which is an excerpt of an algorithm's pseudocode:
if the token is an operator {
while(the stack is not empty
AND the top of the stack is not a "("
AND the precedence of the token on the top of the stack >= current token) {
pop the token on the stack and enqueue it
} // end while
push current token onto the stack
} // end if token is an operator
My approach is this, knowing I am checking for an operator (i.e + - ^ etc..):
while((stack.isEmpty()) == false && (((Comparable<String>)stack.peek()).compareTo( "(") != 0) && ) //Missing a logical && (CHECK ALGORITHM)
queue.enqueue(stack.pop());
stack.push(tokenIterator);
}
Some clarifications:
I wrote my own Stack and Queue classes and including their functionalities and methods. It was part of the assignment. I am not able to use the default Java Stack and Queue structures (as I said, it was part of the assignment to create our own, and replicate their functionalities). I AM 200% such classes work. I thoroughly tested and debugged them, so don't worry about such thing.
Both the Stacks and Queues are of type
<String>
The stack is an instance of my Stack class, that calls
isEmpty()
and returns a boolean (true if empty, false otherwise).The
stack.peek()
call returns an Object, which in this instance is the String at the top of the stack.The application of the entire algorithm is to process the input of a mathematical expression in infix form and convert it into postfix form.
Main problem: I am missing that last bit of pseudocode. To be specific, the line: "AND the precedence of the token on the top of the stack >= current token". I don't understand what it is asking me to check for, therefore I haven't coded it (as you can see, my java code above is missing the code after that last && ). My best guess is that it is asking me to check whether the token that is underneath the top of the stack (aka the element before the top) is >= than the current token, which has to be an operator (otherwise, we wouldn't have entered the upper if statement). I don't know how I can compare say an operator against a number and get something reasonable that does not crash.
Example: I wrote a helper function that prints the current queue contents in a nice form. If I enter the expression (6 + 9) - (9 + 6 + 3) + 2
, the queue prints:
ADD->|| 6| 9 | + | 9 | 6 | + | 3 | + | 2 | + | - ||<-RMV
Which of course is the wrong postfix notation of the above infix expression. According to me, it should be: 6 9 + 9 6 + 3 + - 2 +
You don't really need to know much more, since my issue is merely what does "AND the precedence of the token on the top of the stack >= current token" mean. I cannot understand what it is asking me to do. Hopefully solving this third && (and) condition in my while loop will make my algorithm output the correct postfix notation.
EDIT 1:
Now that I know what precedence is, I think that the (
in my queue may play an important role in checking for precedence, therefore, here is the raw output of the queue for the same math expression given as an example above (I deleted the (
from the queue in the other queue print above because I thought they were useless):
#######QUEUE#######
======================================================================
ADD->|| 6| 9 | + | 9 | 6 | + | 3 | + | 2 | + | ( | - | ( ||<-RMV
======================================================================