krahets/hello-algo · error

両端キューが空です

Error message

両端キューが空です

What it means

This panic is raised by the internal pop(is_front) method of a doubly-linked-list deque (Japanese localization). It fires when dequeuing (popFirst or popLast) from an empty deque. Because the method dereferences front.?.val / rear.?.val, skipping the guard would be unsafe, so the library aborts instead. @panic is uncatchable.

Source

Thrown at ja/codes/zig/chapter_stack_and_queue/linkedlist_deque.zig:100

                node.prev = self.rear;
                self.rear = node;   // 末尾ノードを更新する
            }
            self.que_size += 1;      // キューの長さを更新
        } 

        // キュー先頭にエンキュー
        pub fn pushFirst(self: *Self, num: T) !void {
            try self.push(num, true);
        } 

        // キュー末尾にエンキュー
        pub fn pushLast(self: *Self, num: T) !void {
            try self.push(num, false);
        } 
        
        // デキュー操作
        pub fn pop(self: *Self, is_front: bool) T {
            if (self.isEmpty()) @panic("両端キューが空です");
            var val: T = undefined;
            // キュー先頭からの取り出し
            if (is_front) {
                val = self.front.?.val;     // 先頭ノードの値を一時保存
                // 先頭ノードを削除
                var fNext = self.front.?.next;
                if (fNext != null) {
                    fNext.?.prev = null;
                    self.front.?.next = null;
                }
                self.front = fNext;         // 先頭ノードを更新する
            // キュー末尾からの取り出し
            } else {
                val = self.rear.?.val;      // 末尾ノードの値を一時保存
                // 末尾ノードを削除
                var rPrev = self.rear.?.prev;
                if (rPrev != null) {
                    rPrev.?.next = null;

View on GitHub (pinned to 69932aed18)

Solutions

  1. Check deque.isEmpty() before popFirst()/popLast().
  2. Track counts so the two-ended pops never exceed total pushes.
  3. Guard at the call site: if (!d.isEmpty()) _ = d.popFirst();
  4. Refactor pop to return ?T if you need recoverable empty-pops.

Example fix

// before
var v = deque.popFirst(); // @panics when empty

// after
if (deque.isEmpty()) return;
var v = deque.popFirst();
Defensive patterns

Strategy: validation

Validate before calling

// Validate non-empty before dequeuing either end
if (!deque.isEmpty()) {
    var v = deque.popFirst();
}

Prevention

When it happens

Trigger: Calling deque.popFirst() or deque.popLast() when the deque is empty; or any code that calls pop(true)/pop(false) with zero elements.

Common situations: Deque-based sliding window that pops after the window empties; palindrome/stepping algorithms that pop from both ends and overshoot; producer/consumer mismatch where pops exceed pushes.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/e6d6d2c81bc94436. Report an issue: GitHub.