Please help! How do you create your own array based stack, but not use java\'s b
ID: 3667390 • Letter: P
Question
Please help! How do you create your own array based stack, but not use java's built in stack? This is for building a arthimetic calculator using this pseudo code
Algorithm for Evaluating Expressions Slide by Matt Stallmann included with permission. Two stacks: Algorithm EvalExpO Input: a stream of tokens representing opStk holds operators valStk holds values an arithmetic expression (with numbers) a Use $as special "end of input" utput: the value of the expression token with lowest precedence Algorithm doop) while there's another token z x vaistk.pop(); y vaistk. pop(); op opStk.pop0: valStk.push( y op x) if isNumber(z) then valStk.push(z) else Algorithm repeatOps(refop) while (valStk.size) > 1 A opStk.push(z) prec(refOp) s prec(opStk.top)) return valStk.top) doOp0 © 2014 Goodrich, Tamassia, Goldwasser StacksExplanation / Answer
This is to implement my own array based stack for calculating the capacity in a given tank:
public class Stack_implementation_using array {
private int top;
private int[] tank_capacity;
Stack(int capacity) {
if (capacity <= 0)
throw new IllegalArgumentException(
" The Stack's capacity must be positive and never be negative");
tank_capacity = new int[capacity];
top = -1;
}
void push(int value) {
if (top == tankcapacity.length)
throw new StackException(" Here the Stack's underlying storage is overflow here since capacity gets exceeded");
top++;
tankcapacity[top] = value;
}
int peek() {
if (top == -1)
throw new StackException(" The stack is empty over there");
return tankcapacity[top];
}
void pop() {
if (top == -1)
throw new StackException("The stack is empty over here too");
top--;
}
boolean isEmpty() {
return (top == -1);
}
public class StackException extends RuntimeException {
public StackException(String message) {
super(message);
}
}
}
}