forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list.ts
More file actions
70 lines (60 loc) · 1.24 KB
/
Copy pathlinked-list.ts
File metadata and controls
70 lines (60 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export class LinkedList<T extends {next: T | null}> {
constructor(private _head: T) {}
get(l: number) {
let c: T | null = this._head;
while (c && l > 0) {
l--;
c = c.next;
}
return c;
}
get head() { return this._head; }
get length() {
let c: T | null = this._head;
let i = 0;
while (c) {
i++;
c = c.next;
}
return i;
}
reduce<R>(accumulator: (acc: R, value: T, index?: number) => R, seed: R) {
let c: T | null = this._head;
let acc = seed;
let i = 0;
while (c) {
acc = accumulator(acc, c, i);
i++;
c = c.next;
}
return acc;
}
find(predicate: (value: T, index?: number) => boolean) {
let c: T | null = this._head;
let i = 0;
while (c) {
if (predicate(c, i)) {
break;
}
i++;
c = c.next;
}
return c;
}
forEach(visitor: (value: T, index?: number) => void) {
let c: T | null = this._head;
let i = 0;
while (c) {
visitor(c, i);
i++;
c = c.next;
}
}
}