{"record":{"id":"3ba001884de08449","repo":"krahets/hello-algo","slug":"error-3ba001","errorCode":null,"errorMessage":"стек пуст","messagePattern":"стек пуст","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ru/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts","lineNumber":39,"sourceCode":"    }\n\n    /* Проверка, пуст ли стек */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* Поместить в стек */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* Извлечь из стека */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('стек пуст');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* Доступ к верхнему элементу стека */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('стек пуст');\n        return this.stackPeek.val;\n    }\n\n    /* Преобразовать связный список в Array и вернуть */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/typescript/chapter_stack_and_queue/linkedlist_stack.ts#L21-L57","documentation":"Thrown by pop() on a linked-list stack when this.stackPeek is null/undefined. The message ('стек пуст') guards the reassignment this.stackPeek = this.stackPeek.next. Note peek() is invoked first inside pop() and already throws on an empty stack, making this a redundant secondary guard.","triggerScenarios":"Calling pop() on a stack whose stackPeek pointer is null (nothing pushed, or all popped); unbalanced push/pop sequences in backtracking or expression evaluation.","commonSituations":"DFS/recursion-emulation that pops past the base; evaluator loops that pop operands without verifying availability; reusing a stack object after draining it.","solutions":["Check stack.isEmpty() before pop().","Bound the pop count by stack.size() or use while (!stack.isEmpty()).","Catch the error when an empty pop is recoverable.","Verify push/pop balance in the surrounding algorithm."],"exampleFix":"// before\nconst top = stack.pop(); // throws when stackPeek is null\n\n// after\nconst top = stack.isEmpty() ? null : stack.pop();","handlingStrategy":"validation","validationCode":"if (!stack.isEmpty()) {\n    const top = stack.pop();\n}","typeGuard":null,"tryCatchPattern":"try {\n    const top = stack.pop();\n} catch (e) {\n    if (e instanceof Error && e.message === 'стек пуст') {\n        // empty stack; handle gracefully\n    } else throw e;\n}","preventionTips":["Check isEmpty() before pop().","Verify push/pop balance in backtracking algorithms.","Bound pop loops by the current size."],"tags":["stack","typescript","linked-list","empty-state","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}