krahets/hello-algo · error · Error
キューが空です
Error message
キューが空です
What it means
Thrown by pop() on the linked-list-backed queue when `this.front` is null. Defensive duplicate: pop() first calls peek() (which already throws 'キューが空です' when size===0), so reaching this line means the front pointer is null even though size is non-zero — an inconsistent internal state. Practically, callers see this only if peek's precondition somehow passed but front is still null.
Source
Thrown at ja/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts:49
push(num: number): void {
// 末尾ノードの後ろに num を追加
const node = new ListNode(num);
// キューが空なら、先頭・末尾ノードをともにそのノードに設定
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
- Guard pop() with `if (queue.size > 0)` (or isEmpty if exposed) — this prevents the peek throw, which is the realistic path.
- Avoid mutating front/rear/queSize from outside the class; treat the list as opaque.
- If subclassing, preserve the front===null ⇔ queSize===0 invariant.
Example fix
// before const v = queue.pop(); // after const v = queue.size > 0 ? queue.pop() : undefined;
Defensive patterns
Strategy: validation
Validate before calling
// Guard queue pop (peek throws first with the same message on empty).
if (queue.size > 0) {
const v = queue.pop();
} Type guard
function linkedQueueCanPop(queue) {
return queue.size > 0;
} Try / catch
try {
const v = queue.pop();
} catch (e) {
if (e instanceof Error && e.message === 'キューが空です') {
// empty (or invariant violated)
} else throw e;
} Prevention
- Gate pop on `queue.size > 0`; this prevents the peek throw which is the normal path.
- Never mutate front/rear/queSize from outside the class.
- Keep front===null ⇔ queSize===0 invariant when subclassing.
- If you see this line reached with size>0, suspect external state corruption.
When it happens
Trigger: Calling pop() on an empty queue (peek throws first with the same message); corrupting internal state by manipulating front/queSize directly; subclassing LinkedListQueue and bypassing encapsulation.
Common situations: Normal empty-queue pop surfaces the same message from peek(); the pop-level check is a safety net for invariant violation. Debugging this exact line usually points at external mutation of front/rear/queSize fields.
Related errors
AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13).
Data as JSON: /api/errors/9bbe6509e0977708.
Report an issue: GitHub.