. . . . . . | | +---+ a[2]: | | +---+ a[1]: | Y | top_elt_index: 1 +---+ bottom: a[0]: | X | +---+
public class StackO implements Stack_Interface { private Object[] stackArray; private int top_elt_index; public StackO(int s) { stackArray = new Object[s]; top_elt_index = -1; } public boolean isEmpty() { return top_elt_index == -1; } . . . . . . | | +---+ a[2]: | | +---+ a[1]: | Y | top_elt_index: 1 +---+ a[0]: | X | +---+ public void push(Object o) { stackArray[ ++ top_elt_index ] = o; } . . . . . . | | +---+ a[2]: | Z | top_elt_index: 2 +---+ a[1]: | Y | +---+ a[0]: | X | +---+ public Object peek() { return stackArray[top_elt_index]; } public Object pop() { return stackArray[top_elt_index--]; } . . . . . . | | +---+ a[2]: | | +---+ a[1]: | Y | top_elt_index: 1 +---+ a[0]: | X | +---+ . . . }
PostfixEvaluator
Postfix
InfixApp
infix.java
PostfixApp
postfix.java
public class StackO implements Stack_Interface { private Object[] stackArray; private int top_elt_index; public StackO(int s) { stackArray = new Object[s]; top_elt_index = -1; } // O() i.e. O() public boolean isEmpty() { return top_elt_index == -1; } // O() public Object peek() { return stackArray[top_elt_index]; } // O() public Object pop() { return stackArray[top_elt_index--]; } // O() public push(Object o) { stackArray[ ++ top_elt_index ] = o; } // O() }
/** * @author derived from Robert Lafore */ // requires |class LinkList| to use a |class Link| (node) // which accepts |Object|s as data, and for which |deleteFirst()| // returns the data class LinkStack { private LinkList theList; public LinkStack() // constructor { theList = new LinkList(); } public void push(Object j) // put item on top of stack { theList.insertFirst(j); } public Object pop() // take item from top of stack { return theList.deleteFirst(); } public boolean isEmpty() // true if stack is empty { return theList.isEmpty(); } }