Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Arrays Linked Lists and Stacks

(a.k.a. How to Store Stuff Without Losing Your Mind)

You’ve heard the phrase “Don’t put all your eggs in one basket”? Well, programmers ignored that — and then argued for decades about which basket is best. 🧺💻

Welcome to the world of Arrays, Linked Lists, and Stacks — three different ways to keep track of your stuff, each with their own unique personality disorder.


🧩 Arrays: The Overachieving Sibling

An array is like a perfectly organized shelf in a grocery store. Each item has a specific position (index), and you can find anything in an instant — as long as you remember where it is.

## Array example (in Python, it's just a list)
fruits = ["apple", "banana", "cherry"]
print(fruits[1])  # "banana"

Arrays are:

  • Fast for access (O(1))

  • Awkward when resizing (like buying one extra shoe)

  • Strict about data types (in other languages, not Python — Python’s like “I accept all fruit.”)

Business analogy: Arrays are like corporate cubicles. Everyone has a fixed seat, no one can move, and adding a new employee means rearranging the entire office.

Fun Fact: In C, if you go “out of bounds” in an array, your program doesn’t crash — it just goes rogue. Somewhere, your array is eating another variable’s lunch.


🪢 Linked Lists: The Social Network of Data

A Linked List is like a conga line of data — each node holds a value and a pointer to the next node. No fixed order, no fixed size, just pure, flexible chaos.

## Simple linked list node in Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

## Create nodes and link them
head = Node("apple")
head.next = Node("banana")
head.next.next = Node("cherry")

Traversal? You have to go through the entire line to find your person. Kind of like trying to find who started a rumor at work.

Pros:

  • Super flexible — easy to add or remove items.

  • Perfect when you don’t know how much data you’ll have.

Cons:

  • Slow to access random elements.

  • You can’t just “jump” to index 10 — you have to walk there.

Business analogy: Linked Lists are like startups. Everyone knows the next person, but if one leaves, you might lose the entire chain of command.


🥞 Stacks: The Pancake Principle

A Stack is a simple data structure that follows LIFOLast In, First Out. Think of it like a stack of pancakes 🥞: The last pancake you put on top is the first one you eat (or drop on the floor).

stack = []

## Push items (add to top)
stack.append("plate 1")
stack.append("plate 2")

## Pop item (remove from top)
top = stack.pop()
print(top)  # "plate 2"

Stacks are used for:

  • Undo/redo in software

  • Backtracking (like when you regret all your life choices)

  • Function calls (your program literally stacks them)

Business analogy: Stacks are like a pile of documents on your boss’s desk — only the top one gets attention, the rest slowly fossilize underneath.

Fun Fact: If you call too many recursive functions without stopping, your stack overflows — and your computer basically says,

“You’ve stacked too hard, my friend.” 💥


🧮 Comparing the Trio

FeatureArrayLinked ListStack
Access Speed🔥 Fast (O(1))🐢 Slow (O(n))🧇 Usually via top (O(1))
Memory UsageFixedFlexibleFlexible
InsertionPainfulEasyEasy at top only
DeletionPainfulEasyEasy at top only
AnalogyOffice cubiclesConga linePancake stack

💡 Quick Reality Check

Real-world engineers rarely say,

“We need a doubly linked list of customer invoices!” but understanding these basics makes you smarter when you do things like:

  • Build dynamic arrays (hello, NumPy 👋)

  • Manage memory efficiently

  • Debug recursion gone wild

So whether you’re stacking pancakes, linking friends, or arranging fruit, remember:

Every data structure is just a creative way to say, “I need to keep my mess in order.”

# Your code here

Exercises

Exercise 1


Exercise 2


Exercise 3