{"record":{"id":"37f8462b2542c6e5","repo":"krahets/hello-algo","slug":"error-37f846","errorCode":null,"errorMessage":"стек пуст","messagePattern":"стек пуст","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ru/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/ru/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js#L28-L64","documentation":"Thrown by LinkedListStack.peek (JS) with message 'стек пуст' when #stackPeek is null (no nodes). pop() calls peek() first, so the throw also surfaces from pop() on an empty stack.","triggerScenarios":"Calling peek() or pop() when #stackPeek === null, i.e., the linked list is empty (#stkSize === 0).","commonSituations":"Over-popping a linked-list-backed stack; DFS/recursion simulation that pops more than it pushes; bracket matching closing with an empty stack.","solutions":["Guard on isEmpty()/size before peek or pop.","Treat empty-on-pop as a domain-level 'unbalanced' signal rather than a crash.","Maintain an explicit size invariant alongside the head pointer."],"exampleFix":"// before\nconst v = stack.peek(); // throws when empty\n\n// after\nconst v = stack.isEmpty() ? null : stack.peek();","handlingStrategy":"validation","validationCode":"const v = stack.isEmpty() ? null : stack.pop();","typeGuard":null,"tryCatchPattern":"try { stack.peek(); } catch (e) { if (e.message !== 'стек пуст') throw e; }","preventionTips":["Guard peek/pop with isEmpty() since the head pointer is null when empty.","Treat empty-on-pop in DFS simulation as a domain signal, not a crash.","Keep #stkSize and #stackPeek in sync if you subclass."],"tags":["stack","linked-list","javascript","precondition","empty-state","hello-algo"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}