{"record":{"id":"e4d5ad026004fb41","repo":"krahets/hello-algo","slug":"error-e4d5ad","errorCode":null,"errorMessage":"队列为空","messagePattern":"队列为空","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codes/zig/chapter_stack_and_queue/linkedlist_queue.zig","lineNumber":48,"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.que_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.front.?.val;\n        }  \n\n        // 入队\n        pub fn push(self: *Self, num: T) !void {\n            // 在尾节点后添加 num\n            var node = try self.mem_allocator.create(inc.ListNode(T));\n            node.init(num);\n            // 如果队列为空，则令头、尾节点都指向该节点\n            if (self.front == null) {\n                self.front = node;\n                self.rear = node;\n            // 如果队列不为空，则将该节点添加到尾节点后\n            } else {\n                self.rear.?.next = node;\n                self.rear = node;\n            }\n            self.que_size += 1;","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig#L30-L66","documentation":"This panic is raised by peek() in a singly-linked-list queue (Chinese localization). It fires when reading the front element of a queue whose size is zero. The guard is an explicit size()==0 check followed by @panic, because Zig has no null-return convention and silently returning a zero-value T would hide a caller bug. @panic aborts the program and cannot be caught with try/catch.","triggerScenarios":"Calling queue.peek() on a newly-constructed empty queue, or after pop() has drained all previously-pushed elements.","commonSituations":"BFS/level-order traversal reading the queue front after the last node is dequeued; porting Java/Python queue code that expects a catchable EmptyQueueException; forgetting that a single-element queue becomes empty after one pop.","solutions":["Guard with queue.isEmpty() before every peek() call.","In a consume loop, use while (!queue.isEmpty()) and call peek/pop only inside the loop body.","Cache queue.size() locally and branch on it before reading the front.","Replace peek() with a custom accessor that returns ?T if recoverable semantics are required."],"exampleFix":"// before\nvar head = queue.peek(); // @panics when empty\n\n// after\nwhile (!queue.isEmpty()) {\n    var head = queue.peek();\n    _ = queue.pop();\n}","handlingStrategy":"validation","validationCode":"// Validate non-empty before reading the front\nif (!queue.isEmpty()) {\n    var head = queue.peek();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use while (!queue.isEmpty()) as the consume-loop condition.","Do not assume peek() returns null/zero on empty — it aborts.","Cache queue.size() locally if you read the front conditionally."],"tags":["zig","queue","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"}