Stacks and Queues

Updated: 01 February 2024

Stacks and Queues

  • Collections of objects
  • Some simple operations: insert, remove, iterate, and test if empty
  • Two ways we may modofy data:
    • Stack - pop and push (LIFO)
    • Queue - enqueue and dequeue (FIFO)

Modular Programming

For the purpose of these types of data structures we want to completely separate the details of implementation from the specific client that we want to use. We want to design our libraries so that clients can choose different implementations and allows us to focus on performance where we would like to

Bags, Queues, and Stacks

Stack

A stack stores some data, and should have the following interface:

algorithms/stack/definition.ts
export interface Stack<T> {
  push(value: T): void;
  pop(): T | undefined;
  empty(): boolean;
  size(): number;
}

Linked List

Considering the above, we can implement this using a few different methods. For the first implement this using a linked list:

algorithms/stack/linked-list.ts
import { type Stack } from "./definition";

interface Item<T> {
  value: T;
  next?: Item<T>;
}

const getSize = <T>(item?: Item<T>, count: number = 0): number => {
  if (!item) {
    return count;
  }

  const innerSize = getSize(item.next, count + 1);
  return innerSize;
};

export class LinkedListStack<T> implements Stack<T> {
  private first?: Item<T> = undefined;

  push(value: T): void {
    const oldFirst = this.first;

    this.first = {
      value,
      next: oldFirst,
    };
  }

  pop(): T | undefined {
    const item = this.first;

    this.first = item?.next;

    return item?.value;
  }

  empty(): boolean {
    return this.first === undefined;
  }

  size(): number {
    return getSize(this.first);
  }
}

In this case pushing and popping is a constant time operation

Array

And using an array:

algorithms/stack/fixed-size-array.ts
import type { Stack } from "./definition";

export class FixedSizeArrayStack<T> implements Stack<T> {
  private stack: Array<T>;
  private n = 0;

  constructor(capacity = 10) {
    this.stack = new Array<T>(capacity);
  }

  push(value: T): void {
    this.stack[this.n] = value;
    this.n++;
  }

  pop(): T | undefined {
    this.n--;
    const item = this.stack[this.n];

    return item;
  }

  empty(): boolean {
    return this.n === 0;
  }

  size(): number {
    return this.n;
  }
}

Note that we have used the non-trivial implementation to demonstrate the general concept since in Javascript the builtin array type does everything we would need anyway. As a general note in array based implementations is that we need to consider that an array has a specific capacity that will need to be accounted for when pusing or popping elements. Even though this isn’t really something we need to consider in Javascript the implementation assumes a bit of a more general concept of how arrays work

Considerations

  • Underflow: We probably want to throw if a client tries to pop from an empty stack
  • Overflow: We can use a resizing array to handle this and not require the client to provide us with a capacity
  • Loitering: Currently we still hold a reference to popped items in the stack, we can update our implementation to replace that reference in our array to better handle this

Resizing Arrays

To resize our array we will need to do some kind of copy every time we want to extend our array. Since copying an array is expensive we want to avoid having to do this too frequently. One strategy for choosing when to do this is called repeated doubling in which once we have filled the array we create a new array that is double the size of our old array and copy all the items into that

algorithms/stack/resizing-array.ts
import type { Stack } from "./definition";

export class ResizingArrayStack<T> implements Stack<T> {
  private stack: Array<T>;
  private n = 0;

  constructor() {
    this.stack = new Array<T>(1);
  }

  push(value: T): void {
    if (this.full()) {
      this.resize();
    }

    this.stack[this.n] = value;
    this.n++;
  }

  pop(): T | undefined {
    this.n--;
    const item = this.stack[this.n];

    return item;
  }

  empty(): boolean {
    return this.n === 0;
  }

  size(): number {
    return this.n;
  }

  private full() {
    return this.n >= this.stack.length;
  }

  private resize(): void {
    const size = this.size();
    const newStack = new Array(size * 2);

    for (let i = 0; i < this.stack.length; i++) {
      newStack[i] = this.stack[i];
    }

    this.stack = newStack;
  }
}

In this implementation the cost of inserting an item is N for cases where we are inserting a single item but may be some power of 2 for cases where we are inseting an item that is in a position outside of the current array. Overall however this cost becomes something that we need to pay less frequently as N grows and overall the cost of inserting the first N items comes to be around 3N. This idea of averaging out these costs is called the amortized cost

In the above implementation we do not resize shrink the array when the array is half full since we can end up in a situation where the consumer frequently pushes or pops value around this mark which will cause frequent array resizes and will be very expensive. A bit of a more general solution to this could be to wait until the array is a quarter full and then resize it to half the size which will help avoid some of this resizing

Tradeoffs

  • Linked-list
    • Every operation takes constant time in the worst case
    • Extra time and space for dealing with links
  • Rezing array
    • Every operation takes a constant amortized time
    • Less wasted space

Queue

The definition of a Queue looks very similar to the Stack but the enqueue and dequeue operations work slightly differently

Linked List

In a linked-list implementation we add items at the start of the list and remove them from the end

From an implementation standpoint we also need to keep track of the last and first item in the list

algorithms/queue/linked-list.ts
import { type Queue } from "./definition";

interface Item<T> {
  value: T;
  next?: Item<T>;
}

const getSize = <T>(item?: Item<T>, count: number = 0): number => {
  if (!item) {
    return count;
  }

  const innerSize = getSize(item.next, count + 1);
  return innerSize;
};

export class LinkedListQueue<T> implements Queue<T> {
  private first?: Item<T> = undefined;
  private last?: Item<T> = undefined;

  queue(value: T): void {
    const newNode = {
      value,
    };

    if (this.last) {
      this.last.next = newNode;
    }

    if (this.first === undefined) {
      // if nothing in queue then init to input item
      this.first = newNode;
    }

    this.last = newNode;
  }

  dequeue(): T | undefined {
    const item = this.first;

    this.first = item?.next;

    if (this.first === undefined) {
      // if nothing in queue then we should remove ref to last item
      this.last = undefined;
    }

    return item?.value;
  }

  empty(): boolean {
    return this.first === undefined;
  }

  size(): number {
    return getSize(this.first);
  }
}

Iterators

An iterator is a data structure that implements the iterator and iterable interfaces in the respective language. This enables us to work with in loops and other language specific functionality

Javascript Definition

In Javascript, the relevant definitions look like so:

interface IteratorYieldResult<TYield> {
  done?: false
  value: TYield
}

interface IteratorReturnResult<TReturn> {
  done: true
  value: TReturn
}

type IteratorResult<T, TReturn = any> =
  | IteratorYieldResult<T>
  | IteratorReturnResult<TReturn>

interface Iterator<T, TReturn = any, TNext = undefined> {
  next(...args: [] | [TNext]): IteratorResult<T, TReturn>
}

interface Iterable<T> {
  [Symbol.iterator](): Iterator<T>
}

interface IterableIterator<T> extends Iterator<T> {
  [Symbol.iterator](): IterableIterator<T>
}

Range Iterator

For the purpose of our example, we can implement an iterator that will iterate over a range

algorithms/iterator/range.ts
class RangeIterator implements Iterator<number, number, number> {
  current: number;
  constructor(
    readonly start: number,
    private readonly end: number,
    private readonly step: number
  ) {
    // step back so we can include the initial value
    this.current = start - this.step;
  }

  next(): IteratorResult<number> {
    const outOfBounds = this.current > this.end;
    const canNotIterate = this.current + this.step > this.end;

    const isDone = outOfBounds || canNotIterate;

    if (isDone) {
      return {
        // this return value will be ignored as the iterator is done
        value: undefined,
        done: true,
      };
    }

    this.current += this.step;
    return {
      value: this.current,
      done: false,
    };
  }
}

export class Range implements Iterable<number> {
  constructor(
    private readonly start: number,
    private readonly end: number,
    private readonly step = 1
  ) {}

  [Symbol.iterator]() {
    return new RangeIterator(this.start, this.end, this.step);
  }
}

Bag

In some cases we don’t particularly care about the sequencing but we just want t be able to add items and iterate throught the items that we added in any order - this is called a bag. The interface for how we can define a bag looks like such:

algorithms/iterator/bag.ts
export interface Bag<T> extends Iterable<T> {
  add(item: T): void;
  size(): number;
}

Applications

Lots of languages have the functionality that we discussed above but the reason you may want to implement your own is because these implementations try to address a lot of general cases but may not meet specific requirements that you require for your implementation, for example builtin library code may compromise between read and write operations for a list which may cause a read-heavy algorithm to be very slow unlike a more specialized implementation which can be designed to have more specific performance characteristics to the problem at hand

Dijkstra’s two stack algorithm

THe two stack algorithm for arithmetic operation can be used to evaluate infix expressions. The algorithm is as follows:

Given some input set of strings: ( 1 + ( 2 + 3 ) * ( 4 + 5 ) - 6 ) we can evaluate the result using the following process:

  1. Define two stacks
  • Value stack
  • Operator stack
  1. Iterate over all items
  • If value - push onto value stack
  • If Operator - push onto operator stack
  • If left paren - ignore
  • If right paren - pop two values, pop one operator, and apply the operator, put result onto value stack

The implementation can be seen below:

algorithms/stack/dijkstras-two-stack-arithmetic.ts
import { ResizingArrayStack } from "./resizing-array";

const operate = {
  "+": (a: number, b: number) => a + b,
  "-": (a: number, b: number) => a - b,
  "*": (a: number, b: number) => a * b,
  "/": (a: number, b: number) => a / b,
};

type Operator = keyof typeof operate;

const isDefined = <T>(val: T | undefined): val is T => val !== undefined;

const openParen = "(";
const closeParen = ")";

export const twoStackArithmetic = (strs: string[]) => {
  const values = new ResizingArrayStack<number>();
  const operators = new ResizingArrayStack<Operator>();

  for (const str of strs) {
    switch (str) {
      case openParen:
        break;

      case "+":
      case "-":
      case "*":
      case "/":
        operators.push(str);
        break;

      case closeParen:
        const b = values.pop();
        const a = values.pop();
        const op = operators.pop();

        const valid = isDefined(a) && isDefined(b) && isDefined(op);

        if (!valid) throw new Error("Invalid state");
        const result = operate[op](a, b);

        values.push(result);
        break;

      default:
        const value = parseInt(str);
        values.push(value);
        break;
    }
  }

  return values.pop();
};