{"record":{"id":"1bc1cfd604ae14d3","repo":"krahets/hello-algo","slug":"error-1bc1cf","errorCode":null,"errorMessage":"スタックが空です","messagePattern":"スタックが空です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/codes/python/chapter_stack_and_queue/linkedlist_stack.py","lineNumber":47,"sourceCode":"\n    def push(self, val: int):\n        \"\"\"プッシュ\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"ポップ\"\"\"\n        num = self.peek()\n        self._peek = self._peek.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._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"表示用にリストへ変換\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n\n\n\"\"\"Driver Code\"\"\"\nif __name__ == \"__main__\":\n    # スタックを初期化\n    stack = LinkedListStack()\n","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/python/chapter_stack_and_queue/linkedlist_stack.py#L29-L65","documentation":"This IndexError (Japanese: 'スタックが空です' = 'stack is empty') is raised by LinkedListStack.peek() (linkedlist_stack.py:47) when _size == 0. peek() returns _peek.val; the guard avoids dereferencing _peek (None). pop() calls peek() first, so the same exception propagates through pop() on an empty stack. The linked-list stack is unbounded.","triggerScenarios":"Calling peek() or pop() on a freshly constructed LinkedListStack (_peek is None). Calling pop() more times than push().","commonSituations":"Unbalanced push/pop in DFS/backtracking. Draining the stack then peeking/popping once more. Holding a stale assumption that the stack is non-empty after conditional pushes.","solutions":["Guard with `if not stack.is_empty(): stack.peek()`.","Drain with `while not stack.is_empty(): stack.pop()`.","Track depth explicitly and never pop/peek at depth 0.","Wrap pop in try/except IndexError if empty-pop is benign."],"exampleFix":"// before\ntop = stack.pop()  # IndexError when empty\n\n// after\ntop = stack.pop() if not stack.is_empty() else None","handlingStrategy":"validation","validationCode":"if not stack.is_empty():\n    top = stack.peek()\n# pop() calls peek() — guard pop() identically","typeGuard":"def stack_has_top(stack: LinkedListStack) -> bool:\n    return not stack.is_empty()","tryCatchPattern":"try:\n    top = stack.pop()\nexcept IndexError:\n    top = None","preventionTips":["Guard peek() and pop() with is_empty() (pop delegates to peek).","Drain with `while not stack.is_empty():`.","Track push/pop balance in DFS/backtracking.","The linked-list stack is unbounded, so only underflow can occur."],"tags":["stack","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"}