krahets/hello-algo · error · IndexError
очередь пуста
Error message
очередь пуста
What it means
Raised by LinkedListQueue#peek (ru) on an empty queue. `pop` delegates to peek (`num = peek`), so this same raise surfaces for both `peek` and `pop` when empty. The guard protects `@front.val` from a nil @front.
Source
Thrown at ru/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb:55
@rear.next = node
@rear = node
end
@size += 1
end
### Извлечение из очереди ###
def pop
num = peek
# Удалить головной узел
@front = @front.next
@size -= 1
num
end
### Доступ к элементу в начале очереди ###
def peek
raise IndexError, 'очередь пуста' if is_empty?
@front.val
end
### Преобразовать связный список в Array и вернуть ###
def to_array
queue = []
temp = @front
while temp
queue << temp.val
temp = temp.next
end
queue
end
end
### Driver Code ###
if __FILE__ == $0View on GitHub (pinned to 69932aed18)
Solutions
- Guard with `queue.is_empty?` before peek or pop.
- Drive consumer loops with `until queue.is_empty?`.
- Wrap pop in a helper returning nil on empty for optional semantics.
Example fix
// before queue.pop # raises 'очередь пуста' when empty // after item = queue.is_empty? ? nil : queue.pop
Defensive patterns
Strategy: validation
Validate before calling
item = queue.pop unless queue.is_empty?
Type guard
def queue_nonempty?(q) = !q.is_empty?
Try / catch
begin item = queue.pop # also covers peek rescue IndexError item = nil end
Prevention
- pop routes through peek — guard covers both.
- Drive consumer loops with `until queue.is_empty?`.
- Linked-list queue has no capacity limit — only the empty precondition raises.
When it happens
Trigger: Calling `queue.peek` or `queue.pop` on an empty LinkedListQueue — before any push, or after the last element is dequeued. A consumer that overshoots triggers it.
Common situations: Consumer faster than producer; dequeuing the last item then dequeuing once more; a driver that peeks before the first push.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/82b8625f37dbf20e.
Report an issue: GitHub.