{
    "slug": "queue_data_structure",
    "term": "Queues",
    "category": "data_structures",
    "difficulty": "beginner",
    "short": "A FIFO (First In, First Out) data structure — the first element enqueued is the first dequeued. Used for BFS traversal, job queues, rate limiting, and buffer management.",
    "long": "A queue supports enqueue (add to rear) and dequeue (remove from front), both O(1) with a doubly linked list. PHP's SplQueue implements this. Circular queues reuse space efficiently in fixed-size buffers. Priority queues extend queues by serving the highest-priority element first (SplMinHeap/SplMaxHeap). The distinction matters practically: array_shift() on a large PHP array is O(n) because it reindexes — use SplQueue for O(1) FIFO operations.",
    "aliases": [
        "FIFO",
        "SplQueue",
        "dequeue",
        "circular queue"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "php"
    ],
    "misconception": "A PHP array with array_push/array_shift is an efficient queue — array_shift() reindexes every element, making it O(n); SplQueue or a linked list gives O(1) dequeue.",
    "why_it_matters": "Job queues (Redis, SQS, RabbitMQ) are real-world queues — understanding FIFO ordering explains why jobs process in submission order and why priority queues require special handling.",
    "common_mistakes": [
        "array_shift() for dequeue in a loop — O(n²) total for n dequeues; use SplQueue.",
        "Treating a priority queue like a FIFO queue — priority queues are not ordered by insertion time.",
        "Not handling the empty queue case — dequeue on an empty queue throws RuntimeException.",
        "Using a stack (LIFO) when FIFO order is required — results processed in reverse order."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "stack_data_structure",
        "heap_data_structure",
        "message_queue_patterns",
        "linked_list"
    ],
    "prerequisites": [
        "array_data_structure",
        "big_o_notation",
        "message_queue_patterns"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.splqueue.php"
    ],
    "bad_code": "// O(n²) — array_shift reindexes entire array each call:\n$queue = [];\nfor ($i = 0; $i < 10000; $i++) $queue[] = $i;\nwhile ($queue) {\n    $item = array_shift($queue); // O(n) per call — 10000 calls = O(n²)\n    process($item);\n}",
    "good_code": "// SplQueue — O(1) per operation:\n$queue = new SplQueue();\nfor ($i = 0; $i < 10000; $i++) $queue->enqueue($i);\nwhile (!$queue->isEmpty()) {\n    $item = $queue->dequeue(); // O(1)\n    process($item);\n}",
    "quick_fix": "Use SplQueue for in-memory queues (O(1) enqueue/dequeue) instead of array_shift() which is O(n) — for distributed queues use Redis lists or SQS",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/queue_data_structure",
        "html_url": "https://codeclaritylab.com/glossary/queue_data_structure",
        "json_url": "https://codeclaritylab.com/glossary/queue_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": "[Queues](https://codeclaritylab.com/glossary/queue_data_structure) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/queue_data_structure"
            }
        }
    }
}