{"record":{"id":"6382d93ac8368747","repo":"krahets/hello-algo","slug":"error-6382d9","errorCode":null,"errorMessage":"キューが空です","messagePattern":"キューが空です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/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/ja/codes/python/chapter_stack_and_queue/linkedlist_queue.py#L38-L74","documentation":"This IndexError (Japanese: 'キューが空です' = 'queue is empty') is raised by LinkedListQueue.peek() (linkedlist_queue.py:56) when _size == 0. peek() returns _front.val; the guard avoids dereferencing _front (None). pop() calls peek() first, so the same exception propagates through pop() on an empty queue. The linked-list queue is unbounded, so 'empty' is the only failure mode.","triggerScenarios":"Calling peek() or pop() on a freshly constructed LinkedListQueue (size 0). Calling pop() more times than push(). _front is None until the first push sets both _front and _rear.","commonSituations":"Consumer outrunning producer in a queue-based pipeline. Looping pops by a fixed count rather than by size(). Forgetting pop() delegates to peek() and raises identically.","solutions":["Guard with `if not queue.is_empty(): queue.peek()`.","Drain with `while not queue.is_empty(): queue.pop()`.","Base consume counts on queue.size().","In producer/consumer flows, check is_empty() before consuming."],"exampleFix":"// before\nval = queue.pop()  # IndexError on empty\n\n// after\nval = queue.pop() if not queue.is_empty() else None","handlingStrategy":"validation","validationCode":"if not queue.is_empty():\n    val = queue.peek()\n# pop() calls peek() — guard pop() the same way","typeGuard":"def queue_has_front(q: LinkedListQueue) -> bool:\n    return not q.is_empty()","tryCatchPattern":"try:\n    val = queue.pop()\nexcept IndexError:\n    val = None","preventionTips":["Guard peek() and pop() with is_empty() (pop delegates to peek).","Drain with `while not queue.is_empty():`.","Base consume counts on queue.size().","The linked-list queue is unbounded, so only underflow can occur."],"tags":["queue","python","index-error","empty","linked-list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}