PVM / Python / Data Structures / Stacks

1. Stacks Basics

A stack is a collection of items with restricted access. You can only add items to the top. You can only remove items from the top. This rule is called LIFO (Last In, First Out). You can't remove the middle item or inspect everything freely. It is like a stack of plates. It is like a browser back history. It is like undo/redo in an editor. Python does not have a special Stack type, but lists already support stack behavior. append() -> push to top (end) pop() -> remove from top (end)
 py
# Undo user action (stack example)
undo_stack = []

# User performs actions
undo_stack.append("Type Hello")
undo_stack.append("Type World")
undo_stack.append("Delete World")

# Current actions stack
print("Current stack:", undo_stack)

# User presses Undo
last_action = undo_stack.pop()
print("Undo action:", last_action)

# Current actions stack
print("Current stack:", undo_stack)

"""
    Current stack: ['Type Hello', 'Type World', 'Delete World']
    Undo action: Delete World
    Current stack: ['Type Hello', 'Type World']
"""

2. Custom Stack

Most languages do not provide stacks, but we can create one using list. This is good for enforcing rules and readability. With a raw list, this is posible: stack = [1, 2, 3] stack.insert(0, 99) - breaks stack rules stack[1] = 42 - breaks stack rules With a Stack class: stack.push(1) stack.push(2) stack.push(3) - no way to touch the middle
 py
# Undo functionality (custom stack example)
class Stack:
    def __init__(self):
        self.items = []  # Internal storage

    # Add item to top of stack
    def push(self, item):
        self.items.append(item)  

    # Remove and return top item
    def pop(self):
        if not self.items:
            return None
        return self.items.pop()
    
    # Read top item without removing it
    def peek(self):
        if not self.items:
            return None
        return self.items[-1]
    
# Create stack
undo_stack = Stack()

# Add actions
undo_stack.push("Type: Hello")
undo_stack.push("Type: World")
undo_stack.push("Delete: World")

# Undo last action
undo_stack.pop()

# New top
print("Top:", undo_stack.peek())
print("Stack:", undo_stack.items)

"""
    Top: Type: World
    Stack: ['Type: Hello', 'Type: World']
"""

3. Collections Deque

Similar performance as custom stack implementation. Deque is better for queues, but is also used as stack sometimes. No custom class needed most of the time.
 py
# Browser History Stack (deque)
from collections import deque

# Max number of pages to remember
MAX_HISTORY = 5

# Stack for browser history
history = deque(maxlen=MAX_HISTORY)

# User visits pages
history.append("google.com")
history.append("openai.com")
history.append("gitub.com")
history.append("python.org")
history.append("minte9.com")

print("History:", list(history))

# Visit one more page (oldest is removed automatically) - Look Here
history.append("stackoverflow.com")

print("History:", list(history))

# User click BACK
last_page = history.pop()

print("Back to:", last_page)
print("History:", list(history))

"""
    History: ['google.com', 'openai.com', 'gitub.com', 'python.org', 'minte9.com']
    History: ['openai.com', 'gitub.com', 'python.org', 'minte9.com', 'stackoverflow.com']
    Back to: stackoverflow.com
    History: ['openai.com', 'gitub.com', 'python.org', 'minte9.com']
"""




References: