CS 163 Lecture-Module #12:
Stacks

stack
abstract scheme for holding data (like array)
used in  fundamental for executing modern computer programs
used in 


array implementation of stack:
                .   .
                .   .
                .   .
                |   |
                +---+
         a[2]:  |   |
                +---+
         a[1]:  | Y | top_elt_index: 1
                +---+
bottom:  a[0]:  | X |
                +---+
add items from a[0] up,
have variable to keep track of what's occupied

code   as in textbook:
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 |
                +---+

        .
        .
        .
    }

application:
expression-handling e.g. 3 + 4 * 5
needed in parsing e.g. Java

appears simple/basic
but if you enter 3,+,4,*,5 in calculator, do you get the proper result?


3 + 4 * 5
        what happens with calculator? 35?!?
2 ^ 3 ^ 4
        is it 2^81 or 8^4 ??
3 - 4 - 5 ??
        (3 - 4) - 5
        3 - (4 - 5) ?
also x = y = z
    is x = (y = z) ?
    or is it (x = y) = z ?
when OP is "-" we say TOKEN1 OP TOKEN2 OP TOKEN3
    is (TOKEN1 OP TOKEN2) OP TOKEN3
when OP is "=" we say TOKEN1 OP TOKEN2 OP TOKEN3
    is TOKEN1 OP (TOKEN2 OP TOKEN3)

an alternative to infix is postfix
more clear
vs. infix needing precedences, parentheses, associativity...

textbook presents postfix
e.g. "3 4 + 5 *" instead of "(3 + 4) * 5"
(not to mention the ambiguous "3 + 4 * 5")
calculator actually provides some such capability via
"MS", "M+", "MR"

textbook has InfixApp (infix.java) translating infix to postfix
and PostfixApp (postfix.java) evaluating postfix
I   combined the two into InfixEvalApp in one step evaluating infix
sample input (one-character values, no spaces):
8/2+3*4-5\n

time-analysis:

public class StackO implements Stack_Interface {
    public StackO(int s) {
        stackArray = new Object[s];
        top_elt_index = -1;
        }
        // O(s)  i.e.  O(N)

    public boolean isEmpty() {
        return  top_elt_index == -1;
        }
        // O(1)

    public Object peek() {
        return  stackArray[top_elt_index];
        }
        // O(1)

    public Object pop() {
        return  stackArray[top_elt_index--];
        }
        // O(1)

    public push(Object o) {
        stackArray[ ++ top_elt_index   ] = o;
        }
        // O(1)
    }


(Copyright © 2008 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)