{"record":{"id":"b1e974ec38553d39","repo":"krahets/hello-algo","slug":"error-b1e974","errorCode":null,"errorMessage":"堆疊為空","messagePattern":"堆疊為空","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/python/chapter_stack_and_queue/linkedlist_stack.py#L29-L65","documentation":"Raised by the peek() method of a singly-linked-list-based stack (Traditional Chinese) when the internal _peek pointer is None. Since pop() calls peek() to read the top value before unlinking it (_peek = _peek.next), both operations fail on an empty stack. The guard prevents an AttributeError from dereferencing None.val.","triggerScenarios":"Calling peek() or pop() on a newly constructed LinkedListStack; calling pop() more times than push(); draining the stack completely and then accessing the top once more.","commonSituations":"Backtracking algorithms that pop past the initial state; undo stacks with nothing to undo; expression evaluators that attempt to read an operator from an empty operand stack.","solutions":["Check stack.is_empty() before calling peek() or pop()","Use while not stack.is_empty() as the draining loop condition","Wrap in try/except IndexError for defensive stack operations"],"exampleFix":"# before\nval = stack.peek()\n\n# after\nval = stack.peek() if not stack.is_empty() else None","handlingStrategy":"validation","validationCode":"if not stack.is_empty():\n    val = stack.peek()\nelse:\n    val = None","typeGuard":null,"tryCatchPattern":"try:\n    val = stack.peek()\nexcept IndexError:\n    val = None","preventionTips":["Check is_empty() before peek() or pop() to avoid the guard firing","pop() delegates to peek(), so both share the same guard","The guard prevents AttributeError from dereferencing None.val"],"tags":["data-structure","stack","python","linked-list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}