From 8defe9e09a88b6a099f8714a5fcdfc0c23b84a0b Mon Sep 17 00:00:00 2001 From: YuJian Date: Thu, 12 May 2022 16:22:01 +0800 Subject: [PATCH] vault backup: 2022-05-12 16:22:01 --- .../数据结构 - 链表.md | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/数据结构与算法之美/数据结构 - 链表.md b/数据结构与算法之美/数据结构 - 链表.md index 22de760..1df3e3d 100644 --- a/数据结构与算法之美/数据结构 - 链表.md +++ b/数据结构与算法之美/数据结构 - 链表.md @@ -1,3 +1,6 @@ + + + ## 数组转换成链表 ```typescript @@ -49,4 +52,41 @@ const reverseLinkedList = (LinkedList: LinkedList) => { }; ``` -## 使用链表实现队列 \ No newline at end of file +## 使用链表实现队列 + +```typescript +interface LinkedList { + value: number | null; + next: LinkedList | null; +} + +class LinkedListQueue { + private head: LinkedList | null = null; + private tail: LinkedList | null = null; + private len: number = 0; + + add(value: number) { + if (this.len === 0) { + this.head = { value, next: null }; + this.tail = this.head; + this.len++; + return; + } + this.tail.next = { value, next: null }; + this.tail = this.tail.next; + this.len++; + } + + pop(): number { + if (!this.head || !this.tail) return; + const popValue = this.head.value; + this.head = this.head.next; + this.len--; + return popValue; + } + + get length() { + return this.len; + } +} +``` \ No newline at end of file