{"record":{"id":"3af4d32253343b4e","repo":"krahets/hello-algo","slug":"error-3af4d3","errorCode":null,"errorMessage":"стек пуст","messagePattern":"стек пуст","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ru/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/ru/codes/python/chapter_stack_and_queue/linkedlist_stack.py#L29-L65","documentation":"Raised by the peek() method of a singly-linked-list-based stack (Russian translation) when the stack has zero elements. Since pop() calls peek() internally to retrieve the top value before unlinking it, both operations trigger this error on an empty stack. The guard uses is_empty(), which returns true when the _peek pointer is None or _size equals 0.","triggerScenarios":"Calling peek() or pop() on a freshly constructed LinkedListStack with no prior push() calls; calling pop() more times than push() in a processing loop; draining the stack to completion and then attempting one more access.","commonSituations":"Bracket-matching or expression-evaluation parsers that pop after input is exhausted; recursive-call simulators that pop more frames than were pushed; testing edge cases with empty input arrays passed to stack-based algorithms.","solutions":["Check stack.is_empty() before calling peek() or pop()","Ensure push and pop call counts are balanced inside loops","Wrap the operation in try/except IndexError for defensive handling"],"exampleFix":"# before\nval = stack.pop()\n\n# after\nif not stack.is_empty():\n    val = stack.pop()\nelse:\n    val = None","handlingStrategy":"validation","validationCode":"if not stack.is_empty():\n    val = stack.pop()\n# or equivalently:\nif stack.size() > 0:\n    val = stack.peek()","typeGuard":null,"tryCatchPattern":"try:\n    val = stack.pop()\nexcept IndexError:\n    val = None","preventionTips":["Always check is_empty() before pop() or peek()","Use while not stack.is_empty() as the draining loop condition","Track push() count externally to know when pop() is safe in batch logic"],"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"}