Question: Use a ResizeableArrayStack class instance to implement the algorithm evaluatePostfix GIVEN CODE(JAVA): import java.util.Arrays; import java.util.EmptyStackException public final class ResizableArrayStack implements StackInterface { private T[]

Use a ResizeableArrayStack class instance to implement the algorithm evaluatePostfixUse a ResizeableArrayStack class instance to implement the algorithm evaluatePostfix GIVEN CODE(JAVA):

GIVEN CODE(JAVA):

import java.util.Arrays; import java.util.EmptyStackException

public final class ResizableArrayStack implements StackInterface{ private T[] stack; // Array of stack entries private int topIndex; // Index of top entry private boolean integrityOK; private static final int DEFAULT_CAPACITY = 50; private static final int MAX_CAPACITY = 10000;

public ResizableArrayStack(){ this(DEFAULT_CAPACITY); } public ResizableArrayStack(int initialCapacity) { integrityOK = false; // The cast is safe because the new array contains null entries T[] tempStack = (T[])new Object[initialCapacity]; stack = tempStack; topIndex = -1; integrityOK = true; } // end constructor public void push(T newEntry) { ensureCapacity(); stack[topIndex + 1] = newEntry; topIndex++;

} private void ensureCapacity() { if (topIndex == stack.length - 1) // If array is full, double its size { int newLength = 2 * stack.length; stack = Arrays.copyOf(stack, newLength); } // end if } // end ensureCapacity public T peek() { if (isEmpty()) throw new EmptyStackException(); else return stack[topIndex]; } // end peek public T pop() { if (isEmpty()) throw new EmptyStackException(); else { T top = stack[topIndex]; stack[topIndex] = null; topIndex--; return top; } // end if } // end p public boolean isEmpty() { return topIndex

}

5.18 The evaluation algorithm follows dir Algorithm evaluate Postfix(postfix) // Ae a partiresion valueStack = a new empty stack while (postfix has characters left to parse) { nextCharacter = next nonblank character of postfix switch (nextCharacter) 1 case variable: valueStack.push(value of the variable nextCharacter) break case '': case - case : case '/' : case ase '': operandTwo = valueStack.pop operandOne = valueStack.pop result - the result of the operation in nextCharacter and its operands operandone and operandTwo valueStack.push(result) break default: break // Ignore unexpected characters return valueStack.peek

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!