Stack

A Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. Elements are added and removed from the same end (top). Common operations include push, pop, and peek.

Stack Visualization

Stack is empty

Stack (LIFO): Empty

Complexity

Time Complexity: O(1) for push/pop/peek
Space Complexity: O(n)

Pseudocode

class Stack:
  function push(item):
    stack.append(item)
  
  function pop():
    if stack is empty:
      return error
    return stack.remove_last()
  
  function peek():
    return stack[last_index]