krahets/hello-algo · error · Error
очередь пуста
Error message
очередь пуста
What it means
Thrown by pop() on a linked-list queue when this.front is null/undefined. The message ('очередь пуста') guards the head-node reassignment this.front = this.front.next. Note peek() is called first and already throws on an empty queue, so this redundant guard is a secondary defense.
Source
Thrown at ru/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts:49
push(num: number): void {
// Добавить num после хвостового узла
const node = new ListNode(num);
// Если очередь пуста, сделать так, чтобы и head, и tail указывали на этот узел
if (!this.front) {
this.front = node;
this.rear = node;
// Если очередь не пуста, добавить этот узел после хвостового узла
} else {
this.rear!.next = node;
this.rear = node;
}
this.queSize++;
}
/* Извлечь из очереди */
pop(): number {
const num = this.peek();
if (!this.front) throw new Error('очередь пуста');
// Удалить головной узел
this.front = this.front.next;
this.queSize--;
return num;
}
/* Доступ к элементу в начале очереди */
peek(): number {
if (this.size === 0) throw new Error('очередь пуста');
return this.front!.val;
}
/* Преобразовать связный список в Array и вернуть */
toArray(): number[] {
let node = this.front;
const res = new Array<number>(this.size);
for (let i = 0; i < res.length; i++) {
res[i] = node!.val;View on GitHub (pinned to 69932aed18)
Solutions
- Check queue.isEmpty() (or queue.size === 0) before pop().
- Drive consumption with while (!queue.isEmpty()).
- Catch the error when an empty dequeue is a recoverable signal.
- Ensure enqueue calls precede dequeue calls in the control flow.
Example fix
// before const x = queue.pop(); // throws when front is null // after const x = queue.isEmpty() ? null : queue.pop();
Defensive patterns
Strategy: validation
Validate before calling
if (!queue.isEmpty()) {
const val = queue.pop();
} Try / catch
try {
const val = queue.pop();
} catch (e) {
if (e instanceof Error && e.message === 'очередь пуста') {
// queue empty; handle gracefully
} else throw e;
} Prevention
- Check queue.size === 0 before pop().
- Ensure enqueues precede dequeues in the control flow.
- Use while (!queue.isEmpty()) for full drains.
When it happens
Trigger: Calling pop() on a queue whose front pointer is null (no elements enqueued or all dequeued); a producer/consumer where the consumer dequeues more than was enqueued.
Common situations: BFS where the queue is drained and then popped once more; resetting a queue and forgetting to reset downstream consumers; concurrent/async producers where the consumer races ahead.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/8787689e390903a7.
Report an issue: GitHub.