{"record":{"id":"5935ed7d75149aa7","repo":"krahets/hello-algo","slug":"error-5935ed","errorCode":null,"errorMessage":"キューが空","messagePattern":"キューが空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js","lineNumber":57,"sourceCode":"        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* デキュー */\n    pop() {\n        const num = this.peek();\n        // 先頭ノードを削除\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* キュー先頭の要素にアクセス */\n    peek() {\n        if (this.size === 0) throw new Error('キューが空');\n        return this.#front.val;\n    }\n\n    /* 連結リストを Array に変換して返す */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n\n/* Driver Code */\n/* キューを初期化 */\nconst queue = new LinkedListQueue();","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js#L39-L75","documentation":"Thrown by the linked-list queue's peek when the queue is empty (size === 0). peek reads this.#front.val; without the guard, #front would be null and reading .val would throw a TypeError instead. pop() calls peek() first, so dequeue on an empty queue yields this controlled error.","triggerScenarios":"Calling peek() or pop() on an empty queue (no enqueues, or all dequeued); calling after the front/rear pointers have returned to their initial empty state.","commonSituations":"BFS where the queue drains; producer-consumer imbalance; calling peek at the start of a function before any enqueue.","solutions":["Check queue.size() === 0 (or an isEmpty) before peek()/pop().","Use while (queue.size() > 0) for drain loops.","Return a sentinel when the queue is empty.","Ensure enqueue/dequeue calls are balanced."],"exampleFix":"// before\nconst head = queue.peek();  // throws if empty\n\n// after\nconst head = queue.size === 0 ? null : queue.peek();","handlingStrategy":"validation","validationCode":"function safePeek(queue) {\n  return queue.size === 0 ? null : queue.peek();\n}","typeGuard":"const isNonEmpty = (q) => typeof q.size === 'number' && q.size > 0;","tryCatchPattern":"try {\n  return queue.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === 'キューが空') return null;\n  throw e;\n}","preventionTips":["Check size === 0 before peek()/pop().","Drain with while (queue.size > 0).","Balance enqueue/dequeue calls."],"tags":["queue","linked-list","javascript","empty-state"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}