1
1
import { defaultEquals , IEqualsFunction } from '../util' ;
2
+ import LinkedList from './linked-list' ;
2
3
import { DoublyNode } from './models/linked-list-models' ;
3
4
4
- export default class DoublyLinkedList < T > {
5
- private count = 0 ;
6
- private head : DoublyNode < T > | undefined ;
7
- private tail : DoublyNode < T > | undefined ;
5
+ export default class DoublyLinkedList < T > extends LinkedList < T > {
6
+ protected head : DoublyNode < T > | undefined ;
7
+ protected tail : DoublyNode < T > | undefined ;
8
8
9
- constructor ( private equalsFn : IEqualsFunction < T > = defaultEquals ) { }
9
+ constructor ( protected equalsFn : IEqualsFunction < T > = defaultEquals ) {
10
+ super ( equalsFn ) ;
11
+ }
10
12
11
13
push ( element : T ) {
12
14
const node = new DoublyNode ( element ) ;
13
15
14
- if ( this . head === undefined || this . head === null ) {
16
+ if ( this . head == null ) {
15
17
this . head = node ;
16
18
this . tail = node ; // NEW
17
19
} else {
18
20
// attach to the tail node // NEW
19
- if ( this . tail !== undefined && this . tail !== null ) {
21
+ if ( this . tail != null ) {
20
22
this . tail . next = node ;
21
23
node . prev = this . tail ;
22
24
this . tail = node ;
@@ -25,26 +27,13 @@ export default class DoublyLinkedList<T> {
25
27
this . count ++ ;
26
28
}
27
29
28
- getElementAt ( index : number ) {
29
- if ( index >= 0 && index <= this . count ) {
30
- let node = this . head ;
31
- for ( let i = 0 ; i < index ; i ++ ) {
32
- if ( node ) {
33
- node = node . next ;
34
- }
35
- }
36
- return node ;
37
- }
38
- return undefined ;
39
- }
40
-
41
30
insert ( index : number , element : T ) {
42
31
if ( index >= 0 && index <= this . count ) {
43
32
const node = new DoublyNode ( element ) ;
44
33
let current = this . head ;
45
34
46
35
if ( index === 0 ) {
47
- if ( this . head === undefined || this . head === null ) {
36
+ if ( this . head == null ) {
48
37
// NEW
49
38
this . head = node ;
50
39
this . tail = node ;
@@ -57,19 +46,19 @@ export default class DoublyLinkedList<T> {
57
46
// last item // NEW
58
47
59
48
current = this . tail ; // {2}
60
- if ( current ) {
49
+ if ( current != null ) {
61
50
current . next = node ;
62
51
node . prev = current ;
63
52
this . tail = node ;
64
53
}
65
54
} else {
66
55
const previous = this . getElementAt ( index - 1 ) ;
67
- if ( previous ) {
56
+ if ( previous != null ) {
68
57
current = previous . next ;
69
58
node . next = current ;
70
59
previous . next = node ;
71
60
72
- if ( current ) {
61
+ if ( current != null ) {
73
62
current . prev = node ; // NEW
74
63
node . prev = previous ; // NEW
75
64
}
@@ -86,58 +75,53 @@ export default class DoublyLinkedList<T> {
86
75
let current = this . head ;
87
76
88
77
if ( index === 0 ) {
89
- if ( this . head ) {
78
+ if ( this . head != null ) {
90
79
this . head = this . head . next ; // {1}
91
80
// if there is only one item, then we update tail as well //NEW
92
81
if ( this . count === 1 ) {
93
82
// {2}
94
83
this . tail = undefined ;
95
84
} else {
96
- if ( this . head ) {
85
+ if ( this . head != null ) {
97
86
this . head . prev = undefined ; // {3}
98
87
}
99
88
}
100
89
}
101
90
} else if ( index === this . count - 1 ) {
102
91
// last item //NEW
103
92
current = this . tail ; // {4}
104
- if ( current ) {
93
+ if ( current != null ) {
105
94
this . tail = current . prev ;
106
95
if ( this . tail ) {
107
96
this . tail . next = undefined ;
108
97
}
109
98
}
110
99
} else {
111
100
current = this . getElementAt ( index ) ;
112
- if ( current ) {
101
+ if ( current != null ) {
113
102
const previous = current . prev ;
114
- if ( previous ) {
103
+ if ( previous != null ) {
115
104
// link previous with current's next - skip it to remove
116
105
previous . next = current . next ; // {6}
117
- if ( current && current . next ) {
106
+ if ( current != null && current . next != null ) {
118
107
current . next . prev = previous ; // NEW
119
108
}
120
109
}
121
110
}
122
111
}
123
- if ( current ) {
112
+ if ( current != null ) {
124
113
this . count -- ;
125
114
return current . element ;
126
115
}
127
116
}
128
117
return undefined ;
129
118
}
130
119
131
- remove ( element : T ) {
132
- const index = this . indexOf ( element ) ;
133
- return this . removeAt ( index ) ;
134
- }
135
-
136
120
indexOf ( element : T ) {
137
121
let current = this . head ;
138
122
let index = 0 ;
139
123
140
- while ( current ) {
124
+ while ( current != null ) {
141
125
if ( this . equalsFn ( element , current . element ) ) {
142
126
return index ;
143
127
}
@@ -148,48 +132,35 @@ export default class DoublyLinkedList<T> {
148
132
return - 1 ;
149
133
}
150
134
151
- getHead ( ) {
152
- return this . head ;
153
- }
154
-
155
135
getTail ( ) {
156
136
return this . tail ;
157
137
}
158
138
159
- isEmpty ( ) {
160
- return this . size ( ) === 0 ;
161
- }
162
-
163
139
clear ( ) {
164
- this . head = undefined ;
140
+ super . clear ( ) ;
165
141
this . tail = undefined ;
166
- this . count = 0 ;
167
- }
168
-
169
- size ( ) {
170
- return this . count ;
171
142
}
172
143
173
144
toString ( ) {
174
- if ( this . head === undefined ) {
145
+ if ( this . head == null ) {
175
146
return '' ;
176
147
}
177
148
let objString = `${ this . head . element } ` ;
178
- let current : DoublyNode < T > | undefined = this . head . next ;
179
- while ( current ) {
149
+ let current = this . head . next ;
150
+ while ( current != null ) {
180
151
objString = `${ objString } ,${ current . element } ` ;
181
152
current = current . next ;
182
153
}
183
154
return objString ;
184
155
}
185
156
186
157
inverseToString ( ) {
187
- if ( this . tail === undefined ) {
158
+ if ( this . tail == null ) {
188
159
return '' ;
189
160
}
190
161
let objString = `${ this . tail . element } ` ;
191
- let previous : DoublyNode < T > | undefined = this . tail . prev ;
192
- while ( previous ) {
162
+ let previous = this . tail . prev ;
163
+ while ( previous != null ) {
193
164
objString = `${ objString } ,${ previous . element } ` ;
194
165
previous = previous . prev ;
195
166
}
0 commit comments