{"record":{"id":"9bbe6509e0977708","repo":"krahets/hello-algo","slug":"error-9bbe65","errorCode":null,"errorMessage":"キューが空です","messagePattern":"キューが空です","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts","lineNumber":49,"sourceCode":"    push(num: number): void {\n        // 末尾ノードの後ろに num を追加\n        const node = new ListNode(num);\n        // キューが空なら、先頭・末尾ノードをともにそのノードに設定\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // キューが空でなければ、そのノードを末尾ノードの後ろに追加\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* デキュー */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('キューが空です');\n        // 先頭ノードを削除\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* キュー先頭の要素にアクセス */\n    peek(): number {\n        if (this.size === 0) throw new Error('キューが空です');\n        return this.front!.val;\n    }\n\n    /* 連結リストを Array に変換して返す */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts#L31-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst v = queue.pop();\n\n// after\nconst v = queue.size > 0 ? queue.pop() : undefined;","handlingStrategy":"validation","validationCode":"// Guard queue pop (peek throws first with the same message on empty).\nif (queue.size > 0) {\n  const v = queue.pop();\n}","typeGuard":"function linkedQueueCanPop(queue) {\n  return queue.size > 0;\n}","tryCatchPattern":"try {\n  const v = queue.pop();\n} catch (e) {\n  if (e instanceof Error && e.message === 'キューが空です') {\n    // empty (or invariant violated)\n  } else throw e;\n}","preventionTips":["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."],"tags":["queue","linked-list","empty-state","invariant","typescript"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}