1
- import LinkedListNode from ' ./LinkedListNode' ;
2
- import Comparator from ' ../../utils/comparator/Comparator' ;
1
+ import LinkedListNode from " ./LinkedListNode" ;
2
+ import Comparator from " ../../utils/comparator/Comparator" ;
3
3
4
- export default class LinkedList {
4
+ export default class LinkedList < T > {
5
+ head : LinkedListNode < T > | null ;
6
+ tail : LinkedListNode < T > | null ;
7
+ compare : Comparator < T > ;
5
8
/**
6
9
* @param {Function } [comparatorFunction]
7
10
*/
8
- constructor ( comparatorFunction ) {
11
+ constructor ( comparatorFunction : ( left : T , right : T ) => number = Comparator . defaultCompareFunction ) {
9
12
/** @var LinkedListNode */
10
13
this . head = null ;
11
14
@@ -19,7 +22,7 @@ export default class LinkedList {
19
22
* @param {* } value
20
23
* @return {LinkedList }
21
24
*/
22
- prepend ( value ) {
25
+ prepend ( value : T ) {
23
26
// Make new node to be a head.
24
27
const newNode = new LinkedListNode ( value , this . head ) ;
25
28
this . head = newNode ;
@@ -36,19 +39,23 @@ export default class LinkedList {
36
39
* @param {* } value
37
40
* @return {LinkedList }
38
41
*/
39
- append ( value ) {
42
+ append ( value : T ) {
40
43
const newNode = new LinkedListNode ( value ) ;
41
44
45
+ const { head, tail } = this ;
46
+
42
47
// If there is no head yet let's make new node a head.
43
- if ( ! this . head ) {
48
+ if ( ! head ) {
44
49
this . head = newNode ;
45
50
this . tail = newNode ;
46
51
47
52
return this ;
48
53
}
49
54
50
55
// Attach new node to the end of linked list.
51
- this . tail . next = newNode ;
56
+ if ( tail ) {
57
+ tail . next = newNode ;
58
+ }
52
59
this . tail = newNode ;
53
60
54
61
return this ;
@@ -58,8 +65,9 @@ export default class LinkedList {
58
65
* @param {* } value
59
66
* @return {LinkedListNode }
60
67
*/
61
- delete ( value ) {
62
- if ( ! this . head ) {
68
+ delete ( value : T ) {
69
+ const { head } = this ;
70
+ if ( ! head ) {
63
71
return null ;
64
72
}
65
73
@@ -86,8 +94,9 @@ export default class LinkedList {
86
94
}
87
95
}
88
96
97
+ const tail = this . tail ;
89
98
// Check if tail must be deleted.
90
- if ( this . compare . equal ( this . tail . value , value ) ) {
99
+ if ( tail && this . compare . equal ( tail . value , value ) ) {
91
100
this . tail = currentNode ;
92
101
}
93
102
@@ -100,12 +109,13 @@ export default class LinkedList {
100
109
* @param {function } [findParams.callback]
101
110
* @return {LinkedListNode }
102
111
*/
103
- find ( { value = undefined , callback = undefined } ) {
112
+ find ( f : { value ?: T , callback ?: ( ( ( val : T ) => boolean ) ) } ) {
113
+ const { value, callback } = f ;
104
114
if ( ! this . head ) {
105
115
return null ;
106
116
}
107
117
108
- let currentNode = this . head ;
118
+ let currentNode : LinkedListNode < T > | null = this . head ;
109
119
110
120
while ( currentNode ) {
111
121
// If callback is specified then try to find node by callback.
@@ -142,7 +152,7 @@ export default class LinkedList {
142
152
143
153
// Rewind to the last node and delete "next" link for the node before the last one.
144
154
let currentNode = this . head ;
145
- while ( currentNode . next ) {
155
+ while ( currentNode && currentNode . next ) {
146
156
if ( ! currentNode . next . next ) {
147
157
currentNode . next = null ;
148
158
} else {
@@ -179,7 +189,7 @@ export default class LinkedList {
179
189
* @param {*[] } values - Array of values that need to be converted to linked list.
180
190
* @return {LinkedList }
181
191
*/
182
- fromArray ( values ) {
192
+ fromArray ( values : T [ ] ) {
183
193
values . forEach ( ( value ) => this . append ( value ) ) ;
184
194
185
195
return this ;
@@ -204,8 +214,10 @@ export default class LinkedList {
204
214
* @param {function } [callback]
205
215
* @return {string }
206
216
*/
207
- toString ( callback ) {
208
- return this . toArray ( ) . map ( ( node ) => node . toString ( callback ) ) . toString ( ) ;
217
+ toString ( callback : ( ( val : T ) => string ) | undefined = undefined ) {
218
+ return this . toArray ( )
219
+ . map ( ( node ) => node . toString ( callback ) )
220
+ . toString ( ) ;
209
221
}
210
222
211
223
/**
0 commit comments