Stacks

Nathan Sprague

9/15/21

Stack Interface

Question:

Warm-up Socrative Question

  1. ABCD
  2. DCBA
  3. CDBA
  4. DBA
  5. ABD

Stack Applications

AStack Implementation


    class AStack<E> implements Stack<E> {
      private E stackArray[];         // Array holding stack
      private static final int defaultSize = 10;
      private int maxSize;            // Maximum size of stack
      private int top;                // First free position at top
    
      // Constructors
      @SuppressWarnings("unchecked") // Generic array allocation
      AStack(int size) {
        maxSize = size;
        top = 0; 
        stackArray = (E[])new Object[size]; // Create stackArray
      }
      AStack() { this(defaultSize); }
    
      public void clear() { top = 0; }    // Reinitialize stack
    
    // Push "it" onto stack
      public boolean push(E it) {
        if (top >= maxSize) return false;
        stackArray[top++] = it;
        return true;
      }
    
    // Remove and return top element
      public E pop() {               
        if (top == 0) return null;
        return stackArray[--top];
      }
    
      public E topValue() {          // Return top element
        if (top == 0) return null;
        return stackArray[top-1];
      }
    
      public int length() { return top; } // Return stack size
    }
    
    // Check if the stack is empty
    public boolean isEmpty() { return top == 0; }

    /*
     * OpenDSA Project Distributed under the MIT License
     * 
     * Copyright (c) 2011-2016 - Ville Karavirta and Cliff Shaffer
     */

Dynamic-Array Based AStack

Dynamic-Array Based AStack

Linked Stack Implementation


    // Linked stack implementation
    class LStack<E> implements Stack<E> {
      private Link<E> top;            // Pointer to first element
      private int size;               // Number of elements
    
      // Constructors
      LStack() { top = null; size = 0; }
      LStack(int size) { top = null; size = 0; }
    
      // Reinitialize stack
      public void clear() { top = null; size = 0; }
    
    // Put "it" on stack
      public boolean push(E it) {  
        top = new Link<E>(it, top);
        size++;
        return true;
      }
    
    // Remove "it" from stack
      public E pop() {           
        if (top == null) return null;
        E it = top.element();
        top = top.next();
        size--;
        return it;
      }
    
      public E topValue() {      // Return top value
        if (top == null) return null;
        return top.element();
      }
    
      // Return stack length
      public int length() { return size; }
      
      // Check if the stack is empty
      public boolean isEmpty() { return size == 0; }
    }
    
    /*
     * OpenDSA Project Distributed under the MIT License
     * 
     * Copyright (c) 2011-2016 - Ville Karavirta and Cliff Shaffer
     */

Linked Stack

Linked Stack

Infix and Postfix (Reverse Polish Notation)

Postfix Evaluation Algorithm

for each token:
    if the token is an operand:
        Push the token on the stack
    else: // token is an operator
        Pop two items
        Perform the operation
        Push the result on the stack
return the top of the stack

Exercise

Which of the following represents one of the stack configurations that is reached during evaluation? (Top of the stack is on the left.)

A) 2 4 8 \(\div\) \(\times\)

B) 3 7 1

C) 2 4 8

D) 4 8 1