{"record":{"id":"b6b6e10b55763690","repo":"krahets/hello-algo","slug":"error-b6b6e1","errorCode":null,"errorMessage":"佇列為空","messagePattern":"佇列為空","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/python/chapter_stack_and_queue/array_queue.py","lineNumber":51,"sourceCode":"        # 計算佇列尾指標，指向佇列尾索引 + 1\n        # 透過取餘操作實現 rear 越過陣列尾部後回到頭部\n        rear: int = (self._front + self._size) % self.capacity()\n        # 將 num 新增至佇列尾\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"出列\"\"\"\n        num: int = self.peek()\n        # 佇列首指標向後移動一位，若越過尾部，則返回到陣列頭部\n        self._front = (self._front + 1) % self.capacity()\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._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"返回串列用於列印\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # 初始化佇列\n    queue = ArrayQueue(10)\n\n    # 元素入列","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/python/chapter_stack_and_queue/array_queue.py#L33-L69","documentation":"Raised by the peek() method of a circular-array-based queue when _size equals 0. Since pop() delegates to peek() to read the front element before advancing _front, both operations fail on an empty queue. The _front pointer is meaningless when the queue is empty, so the guard prevents returning stale data.","triggerScenarios":"Calling peek() or pop() on a freshly constructed ArrayQueue with no push() calls; popping all elements and then calling pop() once more; a consumer reading faster than the producer enqueues.","commonSituations":"Producer-consumer pipelines where the queue is temporarily empty; BFS traversals that pop the last node and then call peek; testing with empty input sequences.","solutions":["Check queue.is_empty() before calling peek() or pop()","Use while not queue.is_empty() as the loop condition","Wrap in try/except IndexError for defensive consumer code"],"exampleFix":"# before\nval = queue.peek()\n\n# after\nval = queue.peek() if not queue.is_empty() else None","handlingStrategy":"validation","validationCode":"if not queue.is_empty():\n    val = queue.peek()\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 code","Use while not queue.is_empty() as the draining loop condition","In producer-consumer setups, guard the consumer side with is_empty()"],"tags":["data-structure","queue","python","circular-array"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}