{"record":{"id":"34306589d6c29ac2","repo":"krahets/hello-algo","slug":"queue-is-empty-343065","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/python/chapter_stack_and_queue/linkedlist_queue.py","lineNumber":56,"sourceCode":"            self._rear = node\n        # If the queue is not empty, add the node after the tail node\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"Dequeue\"\"\"\n        num = self.peek()\n        # Delete head node\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"Access front of the queue element\"\"\"\n        if self.is_empty():\n            raise IndexError(\"Queue is empty\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"Convert to list for printing\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # Initialize queue\n    queue = LinkedListQueue()\n\n    # Elements enqueue","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/python/chapter_stack_and_queue/linkedlist_queue.py#L38-L74","documentation":"LinkedListQueue.peek raises IndexError('Queue is empty') when _size is zero, blocking the self._front.val read on a None head. Because pop() calls peek() first, dequeueing from an empty queue surfaces the same exception. The guard encodes that front access requires a non-empty queue.","triggerScenarios":"Calling peek() or pop() on a freshly constructed empty queue; calling after the last node was dequeued; unbounded drain loops; consumer running ahead of producer.","commonSituations":"BFS frontier dequeue; level-order traversal draining the queue; producer/consumer skew; test scaffolding popping more than pushed.","solutions":["Guard single access: `if not queue.is_empty(): head = queue.peek()`.","Bound drains: `while not queue.is_empty(): x = queue.pop()`.","Use queue.size() to control counted loops.","Catch IndexError only for deliberate exception-based flow control."],"exampleFix":"// before\nhead = queue.peek()  # raises if empty\n// after\nhead = queue.peek() if not queue.is_empty() else None","handlingStrategy":"validation","validationCode":"if not queue.is_empty():\n    head = queue.peek()","typeGuard":"def queue_nonempty(q) -> bool:\n    return not q.is_empty()","tryCatchPattern":"try:\n    head = queue.peek()\nexcept IndexError:\n    head = None","preventionTips":["Drive BFS/drains with `while not queue.is_empty()`.","Bound counted loops with size().","Wrap peek in an Optional-returning helper."],"tags":["queue","linkedlist","indexerror","empty-state","python","data-structures"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}