{"record":{"id":"34480cc75beada90","repo":"krahets/hello-algo","slug":"error-34480c","errorCode":null,"errorMessage":"队列为空","messagePattern":"队列为空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"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/codes/typescript/chapter_stack_and_queue/linkedlist_queue.ts#L31-L67","documentation":"Thrown by LinkedListQueue.pop() ('队列为空') when front is null. pop() first calls peek() (which already throws on size 0), then redundantly re-checks !this.front and throws again. Both guards target the empty-queue case; the front-check is a defensive duplicate for the case where size and front disagree.","triggerScenarios":"Calling pop() on an empty queue (size 0 / front null); a consumer loop dequeuing more than was enqueued.","commonSituations":"Unbalanced enqueue/dequeue; a worker reading from a queue fed asynchronously that hits an empty window; calling pop right after construction.","solutions":["Guard with size/isEmpty before pop: if (queue.size > 0) queue.pop().","Use while (queue.size > 0) for draining.","Track the enqueued count and never dequeue past it."],"exampleFix":"// before\nconst v = queue.pop(); // throws when empty\n// after\nif (queue.size > 0) {\n    const v = queue.pop();\n}","handlingStrategy":"validation","validationCode":"function safePop(queue) {\n  return queue.size > 0 ? queue.pop() : undefined;\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Check queue.size > 0 (or front !== null) before pop.","Use while (queue.size > 0) for draining.","Track enqueue/dequeue counts."],"tags":["typescript","queue","linked-list","empty-state","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}