Prefix Evaluation (unlike postfix described in class) has the evaluation strateg
ID: 3803601 • Letter: P
Question
Prefix Evaluation (unlike postfix described in class) has the evaluation strategy of (operation, data, data]. Whenever such a pattern is encountered, the operation is performed on the two data. For example, + 23 45 Indicates that we are performing the addition operation on the values 23 and 45 (the result is 68). Write a pseudocode to obtain the result of a prefix expression using a queue. Your pseudocode should also determine when invalid prefix syntax has occurred. For the example above, the queue Q will look something like: You may also assume all operations given in question 1 are available to you in the queue ADT.Explanation / Answer
1) Queue entire equation
2) Check token.
a) If number, dequeue, then enqueue back on.
b) If operator, check if next two elements are operands, if so, dequeue all three, compute and enqueue result.
c) Else, dequeue, then enqueue.
d) repeat step 2 until only one element left.
3) dequeue result
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.