Stacks, Queues, Deques

Nathan Sprague

Stack ADT (from zyBook)

Operation Description
Push(stack, x) Inserts x on top of stack
Pop(stack) Returns and removes item at top of stack
Peek(stack) Returns but does not remove item at top of stack
IsEmpty(stack) Returns true if stack has no items
GetLength(stack) Returns the number of items in the stack

Queue ADT (from zyBook)

Operation Description
Enqueue(queue, x) Inserts x at end of the queue
Dequeue(queue) Returns and removes item at front of queue
Peek(queue) Returns but does not remove item at the front of the queue
IsEmpty(queue) Returns true if queue has no items
GetLength(queue) Returns the number of items in the queue

Warm-up Socrative Question

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

Deque ADT (from zyBook)

Operation Description
PushFront(deque, x) Inserts x at the front of the deque
PushBack(deque, x) Inserts x at the back of the deque
PopFront(deque) Returns and removes item at front of deque
PopBack(deque) Returns and removes item at back of deque
PeekFront(deque) Returns but does not remove the item at the front of deque
PeekBack(deque) Returns but does not remove the item at the back of deque
IsEmpty(deque) Returns true if the deque is empty
GetLength(deque) Returns the number of items in the deque

Java Stack Interface

public interface Stack<E> {
  
  void push(E item);
  
  E pop();
  
  E peek();
  
  boolean isEmpty();
  
  int size();
  
}

Question:

Stack Applications

ArrayStack Implementation (Modified from zyBook)

public class ArrayStack<E> implements Stack<E> {

  private static final int INITIAL_CAPACITY = 1;

  private E[] array;
  private int size;

  @SuppressWarnings("unchecked")
  public ArrayStack() {
    array = (E[]) new Object[INITIAL_CAPACITY];
    size = 0;
  }

  public void push(E item) {
    if (size == array.length) {
      resize();
    }
    array[size] = item;
    size++;
  }

  public E pop() {
    E toReturn = array[size - 1];
    size--;
    array[size] = null; // WHY????
    return toReturn;

    // return array[--size]; // could be one line.
  }

  @Override
  public E peek() {
    return array[size - 1];
  }

  @Override
  public boolean isEmpty() {
    return size == 0;
  }

  public int size() {
    return size;
  }

  private void resize() {
    int newSize = array.length * 2;
    @SuppressWarnings("unchecked")
    E[] newArray = (E[]) new Object[newSize];
    for (int i = 0; i < size; i++) {
      newArray[i] = array[i];
    }
    array = newArray;
  }

}

Dynamic-Array Based Stack

Dynamic-Array Based Stack

Linked Stack Implementation

public class LinkedStack<E> implements Stack<E>{
  
  private Node top; // No need for a tail reference!
  private int size;
  
  private class Node {
    E item;
    Node next;
    
    public Node(E item, Node next) {
      this.item = item;
      this.next = next;
    }
  }

  @Override
  public void push(E item) {
    top = new Node(item, top);
    size++;
  }

  @Override
  public E pop() {
    E toReturn = top.item;
    top = top.next;
    size--;
    return toReturn;
  }

  @Override
  public E peek() {
    return top.item;
  }

  @Override
  public boolean isEmpty() {
    return size == 0;
  }

  @Override
  public int size() {
    return size;
  }
  
}

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