{"record":{"id":"01b1d0b8d2001040","repo":"krahets/hello-algo","slug":"error-01b1d0","errorCode":null,"errorMessage":"队列为空","messagePattern":"队列为空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"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/codes/javascript/chapter_stack_and_queue/linkedlist_queue.js#L39-L75","documentation":"Thrown by peek() on a singly-linked-list-backed queue when size === 0. peek() returns this.#front.val; on an empty queue #front is null/undefined so .val would crash — the guard returns a clean error instead. pop() calls peek() internally, so both throw identically.","triggerScenarios":"Calling peek()/pop() on a freshly constructed queue; dequeuing more than was enqueued; drain loop without an emptiness check.","commonSituations":"BFS frontier; task queue drained ahead of production; off-by-one in producer/consumer counts; reusing a queue object after it was drained without re-initializing front.","solutions":["Check queue.size > 0 (or isEmpty if exposed) before peek/pop.","Drain with while (queue.size > 0) queue.pop();.","Track enqueue count and never exceed it on dequeue.","Reset/reconstruct the queue rather than reusing a drained one if its internal pointers are suspect."],"exampleFix":"// before\nconst head = q.peek(); // throws if empty\n// after\nconst head = q.size > 0 ? q.peek() : null;","handlingStrategy":"validation","validationCode":"function safePeek(q) {\n  return q.size > 0 ? q.peek() : null;\n}","typeGuard":"function queueNotEmpty(q) {\n  return q.size > 0;\n}","tryCatchPattern":"try {\n  const head = q.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === '队列为空') { /* empty queue */ } else throw e;\n}","preventionTips":["Check q.size > 0 before peek()/pop().","Drain with while (q.size > 0) q.pop();.","Never exceed enqueue count when dequeuing.","Reconstruct the queue after a full drain if pointers are suspect."],"tags":["queue","empty-state","javascript","linked-list"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}