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
Stack Methods pop() push() peek() //return last element without removing size()
public class Stack
{
private LinkedList _innerList;
public Stack()
{
_innerList = new LinkedList();
}
public Node peek(){
// return the tail from Stack without removing
return null;
}
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
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;
}
}