krahets/hello-algo · error
キューが空です
Error message
キューが空です
What it means
This panic is raised by peek() in a singly-linked-list queue (Japanese localization). It fires when reading the front element while que_size == 0. The guard prevents a null front dereference. @panic aborts and is uncatchable.
Source
Thrown at ja/codes/zig/chapter_stack_and_queue/linkedlist_queue.zig:48
// デストラクタ(メモリを解放する)
pub fn deinit(self: *Self) void {
if (self.mem_arena == null) return;
self.mem_arena.?.deinit();
}
// キューの長さを取得
pub fn size(self: *Self) usize {
return self.que_size;
}
// キューが空かどうかを判定
pub fn isEmpty(self: *Self) bool {
return self.size() == 0;
}
// キュー先頭の要素にアクセス
pub fn peek(self: *Self) T {
if (self.size() == 0) @panic("キューが空です");
return self.front.?.val;
}
// エンキュー
pub fn push(self: *Self, num: T) !void {
// 末尾ノードの後ろに num を追加
var node = try self.mem_allocator.create(inc.ListNode(T));
node.init(num);
// キューが空なら、先頭・末尾ノードをともにそのノードに設定
if (self.front == null) {
self.front = node;
self.rear = node;
// キューが空でなければ、そのノードを末尾ノードの後ろに追加
} else {
self.rear.?.next = node;
self.rear = node;
}
self.que_size += 1;View on GitHub (pinned to 69932aed18)
Solutions
- Check queue.isEmpty() (or size()==0) before peek().
- Drive the consumer with while (!queue.isEmpty()).
- Cache size locally and branch before reading front.
- Wrap peek in a helper returning ?T for recoverable use.
Example fix
// before
var head = queue.peek(); // @panics when empty
// after
while (!queue.isEmpty()) {
var head = queue.peek();
} Defensive patterns
Strategy: validation
Validate before calling
// Validate non-empty before reading the front
if (!queue.isEmpty()) {
var head = queue.peek();
} Prevention
- Use while (!queue.isEmpty()) and peek inside.
- Never assume a default value is returned on empty — it aborts.
- Cache size() when conditionally reading the front.
When it happens
Trigger: Calling queue.peek() on a new queue, or after the last element has been dequeued.
Common situations: BFS frontier peek after draining; producer/consumer where consumer reads ahead; porting catchable-exception queue code from another language.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/b33f461c2dcde8a8.
Report an issue: GitHub.