{"record":{"id":"ba940a93db851ed2","repo":"krahets/hello-algo","slug":"error-ba940a","errorCode":null,"errorMessage":"佇列為空","messagePattern":"佇列為空","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/python/chapter_stack_and_queue/linkedlist_queue.py","lineNumber":56,"sourceCode":"            self._rear = node\n        # 如果佇列不為空，則將該節點新增到尾節點後\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"出列\"\"\"\n        num = self.peek()\n        # 刪除頭節點\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"訪問佇列首元素\"\"\"\n        if self.is_empty():\n            raise IndexError(\"佇列為空\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"轉化為串列用於列印\"\"\"\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    # 初始化佇列\n    queue = LinkedListQueue()\n\n    # 元素入列","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/python/chapter_stack_and_queue/linkedlist_queue.py#L38-L74","documentation":"Raised by the peek() method of a singly-linked-list-based queue when the internal _front pointer is None (i.e., _size == 0). Since pop() calls peek() to read the front value before advancing _front = _front.next, both operations fail on an empty queue. Without the guard, pop() would dereference None.next and crash with AttributeError.","triggerScenarios":"Calling peek() or pop() on a freshly constructed LinkedListQueue; draining all elements and then calling pop() once more; a consumer processing items faster than the producer enqueues them.","commonSituations":"BFS traversal that processes all nodes and then attempts one more dequeue; task queues that are momentarily empty between producer batches; testing with empty input collections.","solutions":["Check queue.is_empty() before calling peek() or pop()","Use while not queue.is_empty() as the draining loop condition","Wrap in try/except IndexError for defensive queue operations"],"exampleFix":"# before\nval = queue.pop()\n\n# after\nif not queue.is_empty():\n    val = queue.pop()\nelse:\n    val = None","handlingStrategy":"validation","validationCode":"if not queue.is_empty():\n    val = queue.pop()\nelse:\n    val = None","typeGuard":null,"tryCatchPattern":"try:\n    val = queue.pop()\nexcept IndexError:\n    val = None","preventionTips":["Check is_empty() before peek() or pop() in consumer loops","pop() delegates to peek(), so both share the same guard","Use while not queue.is_empty() as the BFS draining condition"],"tags":["data-structure","queue","python","linked-list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}