|
1 |
| -import { DoublyLinkedList } from "../design-linked-list/DoublyLinkedList.ts"; |
2 |
| - |
3 |
| -export class MyLinkedList<T = number> { |
4 |
| - readonly #freq_list_head = DoublyLinkedList<T>(); |
5 |
| - readonly #freq_list_tail = DoublyLinkedList<T>(); |
6 |
| - constructor() { |
7 |
| - this.#freq_list_head.next = this.#freq_list_tail; |
8 |
| - this.#freq_list_tail.prev = this.#freq_list_head; |
9 |
| - } |
10 |
| - first_node() { |
11 |
| - return this.#freq_list_head.next === this.#freq_list_tail |
12 |
| - ? null |
13 |
| - : this.#freq_list_head.next; |
14 |
| - } |
15 |
| - insert_First(node: DoublyLinkedList<T>): void { |
16 |
| - this.insert_After(this.#freq_list_head, node); |
17 |
| - } |
18 |
| - remove_node(node: DoublyLinkedList<T>): void { |
19 |
| - const prev = node.prev; |
20 |
| - const next = node.next; |
21 |
| - if (prev && next) { |
22 |
| - prev.next = next; |
23 |
| - |
24 |
| - next.prev = prev; |
25 |
| - } else { |
26 |
| - throw Error("next prev node not in list"); |
27 |
| - } |
28 |
| - } |
29 |
| - insert_After( |
30 |
| - prev_node: DoublyLinkedList<T>, |
31 |
| - new_node: DoublyLinkedList<T> |
32 |
| - ): void { |
33 |
| - const next = prev_node.next; |
34 |
| - const prev = prev_node; |
35 |
| - if (prev && next) { |
36 |
| - prev.next = new_node; |
37 |
| - new_node.prev = prev; |
38 |
| - new_node.next = next; |
39 |
| - next.prev = new_node; |
40 |
| - } else { |
41 |
| - throw Error("next prev_node not in list"); |
42 |
| - } |
43 |
| - } |
44 |
| -} |
| 1 | +import { DoublyLinkedList } from "../design-linked-list/DoublyLinkedList.ts"; |
| 2 | + |
| 3 | +export class MyLinkedList<T = number> { |
| 4 | + readonly #freq_list_head = DoublyLinkedList<T>(); |
| 5 | + readonly #freq_list_tail = DoublyLinkedList<T>(); |
| 6 | + constructor() { |
| 7 | + this.#freq_list_head.next = this.#freq_list_tail; |
| 8 | + this.#freq_list_tail.prev = this.#freq_list_head; |
| 9 | + } |
| 10 | + |
| 11 | + first_node() { |
| 12 | + return this.#freq_list_head.next === this.#freq_list_tail |
| 13 | + ? null |
| 14 | + : this.#freq_list_head.next; |
| 15 | + } |
| 16 | + insert_First(node: DoublyLinkedList<T>): void { |
| 17 | + this.insert_After(this.#freq_list_head, node); |
| 18 | + } |
| 19 | + remove_node(node: DoublyLinkedList<T>): void { |
| 20 | + const prev = node.prev; |
| 21 | + const next = node.next; |
| 22 | + if (prev && next) { |
| 23 | + prev.next = next; |
| 24 | + |
| 25 | + next.prev = prev; |
| 26 | + } else { |
| 27 | + throw Error("next prev node not in list"); |
| 28 | + } |
| 29 | + } |
| 30 | + insert_After( |
| 31 | + prev_node: DoublyLinkedList<T>, |
| 32 | + new_node: DoublyLinkedList<T>, |
| 33 | + ): void { |
| 34 | + const next = prev_node.next; |
| 35 | + const prev = prev_node; |
| 36 | + if (prev && next) { |
| 37 | + prev.next = new_node; |
| 38 | + new_node.prev = prev; |
| 39 | + new_node.next = next; |
| 40 | + next.prev = new_node; |
| 41 | + } else { |
| 42 | + throw Error("next prev_node not in list"); |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments