Practice: Shunting Yard

Practice: Shunting Yard#

Question #1: Use an ArrayDeque to declare and initialize a Stack that holds strings. Similarly, declare and initialize a Queue that holds doubles.

Question #2: Write a method reverse that accepts a Queue of Integers as input. It will modify the input queue to be in reverse order. Use only while-loops and a Stack in your implementation.

Question #3: What will the following code output?

Deque<Integer> stack = new ArrayDeque<>();
Deque<Integer> que = new LinkedList<>();
for (int n = 1; n <= 16; n++) {
    que.add(n);
}
while (que.size() > 1) {
    while (!que.isEmpty()) {
        int x = que.remove() + que.remove();
        stack.push(x);
    }
    while (!stack.isEmpty()) {
        int y = stack.pop() - stack.pop();
        que.add(y);
    }
}
System.out.println(que.remove());

Question #4: Evaluate the following postfix expression: 8  2  1  3  +  -  2  *  /

Question #5: Construct the infix expression that matches the following postfix expression: 8  2  1  3  +  -  2  *  /

Question #6: Watch this animated Shunting Yard video and verify that you can predict each step.