Stack

Info

The stack is an ordered container of data that enforces a LIFO (last in, first out) policy on adds and removes.

Think of it like a stack of DVDs. You put in a DVD, and another on top. Then the last DVD you put in will be the first one out, and the first DVD you put in will be the last one out.

Specification

Definition

For a stack holding data type T:

  • init: (no input) -> Stack
    • Pre: true
    • Post: returned value is a new, empty stack
  • isEmpty: (no input) -> bool
    • Pre: true
    • Post: return value is the same as length == 0
  • push: (stack element) -> void
    • Pre: true
    • Post: new stack contains
  • pop: (no input) -> void
    • Pre: is not empty
    • Post: new stack contains
  • peek: (no input) -> T
    • Pre: is not empty
    • Post: true
  • nuke: (no input) -> void
    • Pre: true
    • Post: all items of the stack have been deleted

Implementation

General Implementation with a Linked List

See Linked List.

Runtimes (see Big O):

General Implementation with a Dynamic Array

See Computer Science/Programming/Arrays

Runtimes: