A Stack is a “LIFO” data structure (last in first out).
Here’ s an animation of how stacks are used in an RPN calculator.
More RPN practice
Create a stack Class that is based off of your linkedList code.
A stack has the following 3 methods
1 2 3 4 5 6 |
Stack Methods pop() push() peek() //return last element without removing size() |
1 2 3 4 5 6 7 8 9 |
public class Stack { private LinkedList _innerList; public Stack() { _innerList = new LinkedList(); } |
1 2 3 4 |
public Node peek(){ // return the tail from Stack without removing return null; } |
1 |
public Node pop(){ // remove the tail from the list and return that element return null; } public void push(Node n){ // add the node to the end of the list } } |
Create an RPN calculator Class
1 2 3 4 5 6 7 8 9 10 11 12 |
public class RPNCalc{ // this method computes the value of an // if syntax is invalid, you should throw a ArithmeticException public double evaluate( String rpnString) throws ArithmeticException{ return 0.1; } } |