krahets/hello-algo · error · Error
очередь пуста
Error message
очередь пуста
What it means
Thrown by LinkedListQueue.peek (JS) with message 'очередь пуста' when the queue holds zero nodes. pop() calls peek() first and so propagates the throw on an empty dequeue.
Source
Thrown at ru/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js:57
} else {
this.#rear.next = node;
this.#rear = node;
}
this.#queSize++;
}
/* Извлечь из очереди */
pop() {
const num = this.peek();
// Удалить головной узел
this.#front = this.#front.next;
this.#queSize--;
return num;
}
/* Доступ к элементу в начале очереди */
peek() {
if (this.size === 0) throw new Error('очередь пуста');
return this.#front.val;
}
/* Преобразовать связный список в Array и вернуть */
toArray() {
let node = this.#front;
const res = new Array(this.size);
for (let i = 0; i < res.length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
/* Driver Code */
/* Инициализация очереди */
const queue = new LinkedListQueue();View on GitHub (pinned to 69932aed18)
Solutions
- Gate peek/pop on this.size === 0 check via the queue's size accessor.
- Drain with a captured count: for (let n = q.size; n > 0; n--) q.pop().
- Return null from a wrapper when empty instead of propagating the throw.
Example fix
// before
while (q.size >= 0) { q.pop(); } // last iteration throws
// after
while (q.size > 0) { const v = q.pop(); } Defensive patterns
Strategy: validation
Validate before calling
while (q.size > 0) { const v = q.pop(); process(v); } Try / catch
try { q.pop(); } catch (e) { if (e.message !== 'очередь пуста') throw e; } Prevention
- Drain with while (q.size > 0), not >= 0.
- Capture size once for fixed-count draining loops.
- For BFS, expand the frontier only while the queue is non-empty.
When it happens
Trigger: Calling peek() or pop() when this.size === 0 (no nodes in the linked list).
Common situations: BFS/level-order traversal draining the queue; over-dequeuing relative to enqueue count; consumer faster than producer.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/a087fdb5efb573b9.
Report an issue: GitHub.