1
>
/*---------------------------------------------------------------------------------------------
linkedList.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
class Node<E> {
7
>
8
>
static readonly Undefined = new Node<unknown>(undefined);
9
>
10
>
element: E;
11
>
next: Node<E> | typeof Node.Undefined;
12
>
prev: Node<E> | typeof Node.Undefined;
13
>
14
>
constructor(element: E) {
15
>
this.element = element;
16
>
this.next = Node.Undefined;
17
>
this.prev = Node.Undefined;
18
>
}
19
>
}
20
>
21
>
export class LinkedList<E> {
22
23
private _first: Node<E> | typeof Node.Undefined = Node.Undefined;
24
private _last: Node<E> | typeof Node.Undefined = Node.Undefined;
25
private _size: number = 0;
27
>
get size(): number {
28
return this._size;
29
}
31
>
isEmpty(): boolean {
32
return this._first === Node.Undefined;
33
}
35
>
clear(): void {
36
let node = this._first;
37
while (node !== Node.Undefined) {