{"record":{"id":"9f5f1574a3e24491","repo":"krahets/hello-algo","slug":"error-9f5f15","errorCode":null,"errorMessage":"栈为空","messagePattern":"栈为空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"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/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js#L28-L64","documentation":"Thrown by peek() on a singly-linked-list-backed stack when the internal #stackPeek is null (no nodes). peek() returns #stackPeek.val, which would throw a TypeError without the guard. pop() calls peek() first, so both surface the same error. The check is reference-based (!#stackPeek), equivalent to size === 0.","triggerScenarios":"Calling peek()/pop() on a freshly constructed stack; popping the last node then peeking again; DFS/backtracking unwind past the bottom; balanced-symbol checks on an empty input.","commonSituations":"Monotonic-stack top inspection before any push; expression evaluation with leading operators; recursive helpers that peek before pushing.","solutions":["Guard: if (stack.size > 0) stack.peek(); (the class exposes size via a getter).","Push a sentinel node for algorithms that assume a non-empty stack.","Wrap in a helper returning null on empty rather than catching the throw.","Track push/pop counts and assert balanced usage in tests."],"exampleFix":"// before\nconst t = stack.peek(); // throws if empty\n// after\nconst t = stack.size > 0 ? stack.peek() : null;","handlingStrategy":"validation","validationCode":"function safePeek(stack) {\n  return stack.size > 0 ? stack.peek() : null;\n}","typeGuard":"function stackNotEmpty(stack) {\n  return stack.size > 0;\n}","tryCatchPattern":"try {\n  const t = stack.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === '栈为空') { /* empty stack */ } else throw e;\n}","preventionTips":["Guard peek()/pop() with stack.size > 0.","Push a sentinel node for algorithms assuming non-empty.","Wrap peek() in a null-returning helper.","Track push/pop counts in tests."],"tags":["stack","empty-state","javascript","linked-list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}