Data Structures19 sections · 729 units
Open in Course

Stack Implementation

Array-backed

In most languages, you implement a stack using a dynamic array:

class Stack
    items := empty array

    function push(x)
        append x to items

    function pop()
        return remove last element from items

    function peek()
        return items[length of items - 1]

    function isEmpty()
        return length of items = 0

In practice, use the built-in: C++ has stack, Python has list (use append and pop), Java has Stack or Deque.