{
    "slug": "stack_data_structure",
    "term": "Stacks",
    "category": "data_structures",
    "difficulty": "beginner",
    "short": "A LIFO (Last In, First Out) data structure — the last element pushed is the first popped. Used for function call stacks, undo history, expression parsing, and DFS traversal.",
    "long": "A stack supports three operations: push (add to top), pop (remove from top), peek (read top without removing). All are O(1). PHP's SplStack implements a doubly-linked-list-backed stack. The call stack is the most familiar stack — each function call pushes a frame, each return pops it. Stacks naturally model any 'reverse the order' or 'undo' problem. Bracket matching, postfix expression evaluation, and iterative DFS all use stacks explicitly.",
    "aliases": [
        "LIFO",
        "call stack",
        "SplStack",
        "push pop"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "php"
    ],
    "misconception": "Stacks and queues are interchangeable — stacks are LIFO (last in, first out); queues are FIFO (first in, first out). Reversing an array uses a stack; breadth-first search uses a queue.",
    "why_it_matters": "The PHP call stack is literally a stack — understanding stacks explains stack overflows from deep recursion and how backtracking algorithms work iteratively.",
    "common_mistakes": [
        "Implementing a stack with array_push/array_shift — shift is O(n); use array_push/array_pop (both O(1)).",
        "Not checking isEmpty() before pop — popping an empty stack throws an exception in SplStack.",
        "Using a queue where a stack is needed — the order of processing is reversed.",
        "Confusing stack overflow (too many recursive calls) with heap overflow (memory exhaustion)."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "queue_data_structure",
        "linked_list",
        "recursion_patterns",
        "graph_algorithms"
    ],
    "prerequisites": [
        "array_data_structure",
        "big_o_notation",
        "recursion_patterns"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.splstack.php"
    ],
    "bad_code": "// Using array_shift as 'pop' — O(n) reindex:\n$stack = [];\narray_push($stack, 'a');\narray_push($stack, 'b');\n$top = array_shift($stack); // 'a' — wrong order AND O(n)",
    "good_code": "// array_pop is O(1) — correct LIFO:\n$stack = [];\narray_push($stack, 'a');\narray_push($stack, 'b');\n$top = array_pop($stack); // 'b' — correct LIFO, O(1)\n\n// Or SplStack:\n$stack = new SplStack();\n$stack->push('a');\n$stack->push('b');\n$top = $stack->pop(); // 'b'",
    "quick_fix": "Use PHP's array with array_push/array_pop as a stack (LIFO) — it's O(1) for both operations; use SplStack for explicit stack semantics with a more descriptive interface",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/stack_data_structure",
        "html_url": "https://codeclaritylab.com/glossary/stack_data_structure",
        "json_url": "https://codeclaritylab.com/glossary/stack_data_structure.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Stacks](https://codeclaritylab.com/glossary/stack_data_structure) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/stack_data_structure"
            }
        }
    }
}