krahets/hello-algo · error · IndexError

両端キューは空です

Error message

両端キューは空です

What it means

Raised by LinkedListDeque#pop (ja/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb:70) when the deque is empty. pop(is_front) unlinks either the head or tail node; with size == 0 there is no node, so the guard blocks it. pop_first/pop_last delegate here. Message: "両端キューは空です" (Deque is empty). Japanese mirror of error 543.

Source

Thrown at ja/codes/ruby/chapter_stack_and_queue/linkedlist_deque.rb:70

      node.prev = @rear
      @rear = node # 末尾ノードを更新する
    end
    @size += 1 # キューの長さを更新
  end

  ### キュー先頭に追加 ###
  def push_first(num)
    push(num, true)
  end

  ### キュー末尾に追加 ###
  def push_last(num)
    push(num, false)
  end

  ### デキュー操作 ###
  def pop(is_front)
    raise IndexError, '両端キューは空です' if is_empty?

    # キュー先頭からの取り出し
    if is_front
      val = @front.val # 先頭ノードの値を一時保存
      # 先頭ノードを削除
      fnext = @front.next
      unless fnext.nil?
        fnext.prev = nil
        @front.next = nil
      end
      @front = fnext # 先頭ノードを更新する
    # キュー末尾からの取り出し
    else
      val = @rear.val # 末尾ノードの値を一時保存
      # 末尾ノードを削除
      rprev = @rear.prev
      unless rprev.nil?
        rprev.next = nil

View on GitHub (pinned to 69932aed18)

Solutions

  1. Check deque.is_empty? before pop_first/pop_last.
  2. Verify deque.size is sufficient (>= 1, or >= 2 for paired pops).
  3. Rescue IndexError around the pop and treat empty as the loop's exit.

Example fix

# before
while true
  deque.pop_last # raises once drained
end

# after
until deque.is_empty?
  deque.pop_last
end
Defensive patterns

Strategy: validation

Validate before calling

deque.pop_last unless deque.is_empty?

Type guard

def deque_nonempty?(d); d.respond_to?(:is_empty?) && !d.is_empty?; end

Try / catch

begin
  deque.pop_last
rescue IndexError
  nil
end

Prevention

When it happens

Trigger: Calling deque.pop_first or deque.pop_last when deque.size == 0 — before any push_first/push_last, or after every node has been removed.

Common situations: A two-ended buffer drained past empty; deque-as-worklist fully consumed; test popping in pairs without checking size.

Related errors


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