This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
/
linked-list.ts
343 lines (287 loc) · 7.97 KB
/
linked-list.ts
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import LinkedListNode from './linked-list-node'
import * as utils from '../../utils'
interface List<T> {
head: LinkedListNode<T>
tail: LinkedListNode<T>
size: number
}
class LinkedList<T> implements Iterable<T> {
private list: List<T> | undefined
private equalsF: utils.EqualsFunction<T> = utils.defaultEquals
/**
* Creates a LinkedList - O(1)
* @param equalsFunction equal function for for non-primitive values.
*/
constructor(equalsFunction?: utils.EqualsFunction<T>) {
this.list = undefined
if (equalsFunction) this.equalsF = equalsFunction
}
/*****************************************************************************
INSPECTION
*****************************************************************************/
/**
* Returns size - O(1)
* @return {number}
*/
size(): number {
if (this.list) return this.list.size
return 0
}
/**
* Returns true if inked list is empty, false otherwise - O(1)
* @return {number}
*/
isEmpty(): boolean {
return !this.list
}
/*****************************************************************************
INSERTION
*****************************************************************************/
/**
* Adds node to the head of the linked list - O(1)
* @param {T} val - value to add to list
* @return {void}
*/
addFront(val: T): boolean {
const newNode = new LinkedListNode(val)
if (this.list) {
// link old head backwards
this.list.head.prev = newNode
// link new head forwards
newNode.next = this.list.head
this.list.head = newNode
this.list.size += 1
} else {
this.list = {
head: newNode,
tail: newNode,
size: 1,
}
}
return true
}
/**
* Adds node to the tail of the linked list - O(1)
* @param {T} - value to add to list
* @return {void}
*/
addBack(val: T): boolean {
const newNode = new LinkedListNode(val)
if (this.list) {
// link old tail forwards
this.list.tail.next = newNode
// link new tail backwards
newNode.prev = this.list.tail
this.list.tail = newNode
this.list.size += 1
} else {
this.list = {
head: newNode,
tail: newNode,
size: 1,
}
}
return true
}
/**
* Adds a node at specified index - O(n)
* @param {number} i - index
* @param {T} val - value to add to list
* @return {void}
*/
addAt(i: number, val: T): boolean {
if (i === 0) {
return this.addFront(val)
}
if (i === this.size()) {
return this.addBack(val)
}
if (i < 0 || i >= this.size() || !this.list) return false
let cur = this.list.head
// traverse to index
for (let j = 0; j < i - 1; j++) {
cur = cur.next! // eslint-disable-line
}
const newNode = new LinkedListNode(val)
// link next node
cur.next!.prev = newNode // eslint-disable-line
newNode.next = cur.next
// link prev node
newNode.prev = cur
cur.next = newNode
this.list.size += 1
return true
}
/*****************************************************************************
ACCESSING
*****************************************************************************/
/**
* Gets the value of head - O(1)
* @returns {T} value of head
*/
peekFront(): T | null {
if (!this.list) return null
return this.list.head.val
}
/**
* Gets the value of tail - O(1)
* @returns {T} value of tail
*/
peekBack(): T | null {
if (!this.list) return null
return this.list.tail.val
}
/**
* Gets the element at index i - O(n)
* @param {number} i - index of element
* @returns {T} value of element at index i
*/
get(i: number): T | null {
if (i < 0 || i >= this.size() || !this.list) {
return null
}
let j = 0
let cur = this.list.head
while (j < i) {
cur = cur.next! // eslint-disable-line
j++
}
return cur.val
}
/*****************************************************************************
SEARCHING
*****************************************************************************/
/**
* Removes the first occurrence of the specified item in the linked list.
* @param {T} value - value to search for
* @return {number} the index of the first occurence of the element, and -1
* if the element does not exist.
*/
indexOf(value: T): number {
// list is empty
if (!this.list) return -1
let i = 0
let cur = this.list.head
while (!this.equalsF(cur.val, value)) {
// cur.next === null means we reached end of list without finding element
if (!cur.next) return -1
cur = cur.next
i += 1
}
return i
}
/**
* Checks if value is in linked list.
* @param {T} value - value to search for
* @returns {boolean}
*/
contains(value: T): boolean {
const index = this.indexOf(value)
return index !== -1
}
/*****************************************************************************
DELETION
*****************************************************************************/
/**
* Removes head - O(1)
* @return {T} - value of removed head
*/
removeFront(): T | null {
if (!this.list) return null
// extract val of head so we can return it later
const val = this.list.head.val
if (this.list.head.next) {
// newHead.prev = null
this.list.head.next.prev = null
// move head pointer forwards
this.list.head = this.list.head.next
this.list.size -= 1
} else {
// list is size 1, clear the list
this.list = undefined
}
return val
}
/**
* Removes tail - O(1)
* @return {T} - value of removed head
*/
removeBack(): T | null {
if (!this.list) return null
// extract the val of tail so we can return it later
const val = this.list.tail.val
if (this.list.tail.prev) {
// newTail.next = null
this.list.tail.prev.next = null
// move tail pointer backwards
this.list.tail = this.list.tail.prev
this.list.size -= 1
} else {
this.list = undefined
}
return val
}
/**
* Removes first occurence of node with specified value. Returns true if
* removal was successful, and false otherwise. - O(n)
* @param {T} val - value to remove
* @returns {T} - value of removed node
*/
remove(val: T): T | null {
const index = this.indexOf(val) // O(n)
if (index === -1) return null
return this.removeAt(index) // O(n)
}
/**
* Removes node at specified index- O(n)
* @param {number} i - index to remove
* @return {T} - value of removed node
*/
removeAt(i: number): T | null {
if (!this.list) return null
if (i === 0) {
return this.removeFront()
} else if (i === this.size() - 1) {
return this.removeBack()
}
if (i < 0 || i >= this.list.size) return null
let j = 0
let cur = this.list.head
// traverse to node to be deleted
while (j < i) {
cur = cur.next! // eslint-disable-line
j += 1
}
// delete node
cur.prev!.next = cur.next // eslint-disable-line
cur.next!.prev = cur.prev // eslint-disable-line
this.list.size -= 1
return cur.val
}
/**
* Deletes all nodes - O(1)
*/
clear(): void {
this.list = undefined
}
/*****************************************************************************
HELPERS
*****************************************************************************/
/**
* Appends values from an array to list - O(k)
*/
fromArray(A: T[]): LinkedList<T> {
for (const a of A) {
this.addBack(a)
}
return this
}
*[Symbol.iterator](): Iterator<T> {
if (!this.list) return
let cur: LinkedListNode<T> | null
for (cur = this.list.head; cur != null; cur = cur.next) {
yield cur.val
}
}
}
export default LinkedList