{"record":{"id":"81fdb04913b4d058","repo":"krahets/hello-algo","slug":"error-81fdb0","errorCode":null,"errorMessage":"栈为空","messagePattern":"栈为空","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codes/zig/chapter_stack_and_queue/linkedlist_stack.zig","lineNumber":46,"sourceCode":"        // 析构函数（释放内存）\n        pub fn deinit(self: *Self) void {\n            if (self.mem_arena == null) return;\n            self.mem_arena.?.deinit();\n        }\n\n        // 获取栈的长度\n        pub fn size(self: *Self) usize {\n            return self.stk_size;\n        }\n\n        // 判断栈是否为空\n        pub fn isEmpty(self: *Self) bool {\n            return self.size() == 0;\n        }\n\n        // 访问栈顶元素\n        pub fn peek(self: *Self) T {\n            if (self.size() == 0) @panic(\"栈为空\");\n            return self.stack_top.?.val;\n        }  \n\n        // 入栈\n        pub fn push(self: *Self, num: T) !void {\n            var node = try self.mem_allocator.create(inc.ListNode(T));\n            node.init(num);\n            node.next = self.stack_top;\n            self.stack_top = node;\n            self.stk_size += 1;\n        } \n\n        // 出栈\n        pub fn pop(self: *Self) T {\n            var num = self.peek();\n            self.stack_top = self.stack_top.?.next;\n            self.stk_size -= 1;\n            return num;","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig#L28-L64","documentation":"This panic is raised by peek() in a singly-linked-list stack (Chinese localization). It fires when reading the top element of a stack whose stk_size is zero. The implementation deliberately aborts rather than returning a sentinel, since an empty-stack read is a programmer error. @panic is non-recoverable in Zig.","triggerScenarios":"Calling stack.peek() on a fresh stack, or after pop() has removed the last element.","commonSituations":"Expression-evaluation / parenthesis-matching that peeks the operator stack after it is drained; DFS recursion-elimination where the stack is checked at the wrong point; porting code that relied on catching an EmptyStackException.","solutions":["Check stack.isEmpty() before calling peek().","Use a guarded loop: while (!stack.isEmpty()) { ... } and only peek/pop inside.","Verify stk_size via stack.size() > 0 before any top access.","Wrap the stack in a helper that returns ?T for recoverable use."],"exampleFix":"// before\nvar top = stack.peek(); // @panics when empty\n\n// after\nif (stack.isEmpty()) return;\nvar top = stack.peek();","handlingStrategy":"validation","validationCode":"// Validate non-empty before reading the top\nif (!stack.isEmpty()) {\n    var top = stack.peek();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check stack.isEmpty() before every peek().","Drive stack-consuming loops with while (!stack.isEmpty()).","Remember @panic is uncatchable in Zig — preconditions are the only defense."],"tags":["zig","stack","panic","precondition","data-structure","i18n-zh"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}