{"record":{"id":"c907050133407829","repo":"krahets/hello-algo","slug":"error-c90705","errorCode":null,"errorMessage":"堆疊為空","messagePattern":"堆疊為空","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig#L28-L64","documentation":"Unrecoverable Zig @panic from peek() on the linked-list stack. Guard `if (self.size() == 0) @panic(\"堆疊為空\")` runs before `self.stack_top.?.val`, preventing a null optional unwrap. Peeking an empty stack is treated as a caller logic error.","triggerScenarios":"Call peek() on a LinkedListStack never pushed, or popped down to a null top. Also triggered by nested evaluators/parsers that inspect the top before any push or after the final pop.","commonSituations":"Expression evaluators peeking for a pending operator on an empty operand stack; bracket matchers peeking before pushing; DFS-emulation peeking an empty work stack; tests asserting on the top of a freshly built stack.","solutions":["Guard with `if (!stk.isEmpty())` before every peek().","Restructure so peek() runs only inside a branch that already established size() > 0.","Add a ?T-returning wrapper if empty-top inspection is a legitimate case."],"exampleFix":"// before\nconst top = stk.peek();\n// after\nconst top = if (stk.isEmpty()) null else stk.peek();","handlingStrategy":"validation","validationCode":"// call BEFORE peek()\nif (stk.isEmpty()) return null;\nconst top = stk.peek();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prepend `if (!stk.isEmpty())` to every peek().","Design evaluators/parsers so the top is only peeked after a guaranteed push.","Use a ?T-returning wrapper for stacks where empty-top peeking is normal."],"tags":["zig","stack","peek","data-structure","empty-state"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}