{"record":{"id":"465ed327340994db","repo":"krahets/hello-algo","slug":"error-465ed3","errorCode":null,"errorMessage":"堆疊為空","messagePattern":"堆疊為空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"zh-hant/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js","lineNumber":46,"sourceCode":"    /* 入堆疊 */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* 出堆疊 */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* 訪問堆疊頂元素 */\n    peek() {\n        if (!this.#stackPeek) throw new Error('堆疊為空');\n        return this.#stackPeek.val;\n    }\n\n    /* 將鏈結串列轉化為 Array 並返回 */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n\n/* Driver Code */\n/* 初始化堆疊 */\nconst stack = new LinkedListStack();","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/zh-hant/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js#L28-L64","documentation":"Thrown by LinkedListStack.peek() (message: '堆疊為空' = 'stack is empty') when stackPeek is null. peek() reads stackPeek.val; without the guard a TypeError would occur. pop() delegates to peek(), so popping an empty stack surfaces this error.","triggerScenarios":"Calling pop() or peek() when no nodes have been pushed, or after all nodes were popped.","commonSituations":"Unbalanced push/pop in algorithm code; backtracking past the sentinel; DFS that pops the last element then peeks.","solutions":["Check stack.size > 0 (or !stack.isEmpty()) before pop() or peek().","Ensure push/pop are balanced in expression evaluation and DFS.","Return null instead of calling peek() when the stack is empty."],"exampleFix":"// before\nconst val = stack.pop(); // throws '堆疊為空' (pop calls peek)\n\n// after\nif (stack.size > 0) {\n    const val = stack.pop();\n}","handlingStrategy":"validation","validationCode":"// peek() and pop() (which calls peek) both throw on empty\nif (stack.size > 0) {\n    const val = stack.pop();\n}","typeGuard":null,"tryCatchPattern":"try {\n    const val = stack.pop();\n} catch (e) {\n    if (e.message === '堆疊為空') {\n        // stack is empty — handle underflow\n    } else throw e;\n}","preventionTips":["Check stack.size > 0 before pop() or peek().","Ensure push/pop are balanced in DFS and expression evaluation.","Return null instead of peek() when the stack is empty."],"tags":["stack","linked-list","javascript","empty-state","precondition"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}