CS 163 Lecture-Module #11:
Stacks

stack
abstract scheme for holding data (like array, linked list)
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 precise
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"

our textbook presents  PostfixEvaluator/Postfix  evaluating postfix
another textbook had  InfixApp (infix.java) translating infix to postfix
and  PostfixApp (postfix.java) evaluating postfix
I ~~combined the two into InfixEvalApp which in one step evaluates infix
sample input (single-digit~numbers; no spaces):
8/2+3*4-5\n

time analysis:

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()
    }


alternatively stack class can use a linked list to hold data
instead of using an array which has fixed size
    which could be too big or worse too small
    changing array size would be annoying
to use linked list, stack class's constructor and other methods
do linked-list accessing analogous to above array accessing,
as follows:
/**
 * @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();
      }
   }


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