|
|
|
@ -1,3 +1,6 @@
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 数组转换成链表 |
|
|
|
|
|
|
|
|
|
```typescript |
|
|
|
@ -49,4 +52,41 @@ const reverseLinkedList = (LinkedList: LinkedList) => {
@@ -49,4 +52,41 @@ const reverseLinkedList = (LinkedList: LinkedList) => {
|
|
|
|
|
}; |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
## 使用链表实现队列 |
|
|
|
|
## 使用链表实现队列 |
|
|
|
|
|
|
|
|
|
```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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
``` |