need some java help

nburvi1

Solid State Member
Messages
6
i just dont understand queues

Stacks, Queues and Prefix Evaluation. A prefix expression is one in which we write the operation before the operands; consider the following examples.

Infix Prefix
4 + 5 + 4 5
1 + 2 * 3 + 1 * 2 3
1 + 2 + 13 + 4 + 5 + 16 + + + + + 1 2 13 4 5 16
Invalid + 1
Invalid + 1 2 3

This exercise is designed to let you learn to use a queue and a stack class.

Implement method enqueuePrefix(), which stores the characters input from the keyboard into a queue.

Implement method evaluatePrefix(), which takes a prefix arithmetic expression from the queue and evaluates it using a stack.

For example, for the input " + 1 * 2 0 ", your output (printed on screen) should be 1.

To simplify this problem, you may assume that + and * are the only operators.
Space is the separator.
You should print out error message for an illegal prefix expression.

Hint: 1. You can directly use the LinkedList class as a queue, or you can define a new Queue class, which inherits from LinkedList but with fewer methods.
2. To evaluate a prefix expression, please consult the lecture notes on postfix evaluation and then design a similar evaluation algorithm for prefix expressions.
Sample Run:

Problem 1:
Enter string:
1 2 3 4 5
The reversed string:
5 2 3 4 1

Problem 2:
Enter a prefix expression:
+ 3 11
Value:
14

Enter a prefix expression:
+ + 3 11
Value:
Invalid
 
Back
Top Bottom