Skip to content

Commit 2986281

Browse files
authored
feat: linked-list add length property (#8)
* feat: linked-list add length property * perf: expose length property * fix: length getter should be public
1 parent abdc3f0 commit 2986281

File tree

2 files changed

+53
-15
lines changed

2 files changed

+53
-15
lines changed

src/linked-list/__tests__/index.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@ describe('LinkedList class', () => {
1414
expect(linkedList).toBeInstanceOf(Object)
1515
expect(linkedList.head).toBe(null)
1616
expect(linkedList.tail).toBe(null)
17+
expect(linkedList.length).toBe(0)
1718
})
1819

1920
describe('Method append()', () => {
20-
it('should be a chain method', () => {
21-
linkedList
22-
.append(1)
23-
.append(2)
24-
.append(3)
25-
expect(linkedList.head).toBeDefined()
26-
expect(linkedList.tail).toBeDefined()
27-
expect((linkedList.head as Node).value).toBe(1)
28-
expect((linkedList.tail as Node).value).toBe(3)
29-
})
30-
3121
it('should append a node to linkedlist', () => {
22+
expect(linkedList.length).toBe(0)
3223
linkedList.append(1)
24+
expect(linkedList.length).toBe(1)
3325

3426
expect(linkedList.head).toBeDefined()
3527
expect(linkedList.head).toBeInstanceOf(Node)
@@ -44,6 +36,18 @@ describe('LinkedList class', () => {
4436
expect(linkedList.head).toBe(linkedList.tail)
4537
})
4638

39+
it('should be a chain method', () => {
40+
linkedList
41+
.append(1)
42+
.append(2)
43+
.append(3)
44+
expect(linkedList.head).toBeDefined()
45+
expect(linkedList.tail).toBeDefined()
46+
expect((linkedList.head as Node).value).toBe(1)
47+
expect((linkedList.tail as Node).value).toBe(3)
48+
expect(linkedList.length).toBe(3)
49+
})
50+
4751
it('should append 2 values be linked', () => {
4852
linkedList.append(1).append(2)
4953

@@ -90,6 +94,7 @@ describe('LinkedList class', () => {
9094
linkedList.prepend(2)
9195

9296
expect(linkedList.toString()).toBe('2,1')
97+
expect(linkedList.length).toBe(2)
9398
})
9499

95100
it('should handle head & tail correctly', () => {
@@ -102,6 +107,7 @@ describe('LinkedList class', () => {
102107
expect(linkedList.toString()).toBe('1,2,3,4,5')
103108
expect((linkedList.head as Node).value).toBe(1)
104109
expect((linkedList.tail as Node).value).toBe(5)
110+
expect(linkedList.length).toBe(5)
105111
})
106112
})
107113

@@ -152,6 +158,7 @@ describe('LinkedList class', () => {
152158

153159
it('should return null if the list is empty()', () => {
154160
expect(linkedList.delete(1)).toBe(null)
161+
expect(linkedList.length).toBe(0)
155162
})
156163

157164
it('should delete nothing if not found value', () => {
@@ -162,6 +169,7 @@ describe('LinkedList class', () => {
162169

163170
const deletedNode = linkedList.delete(6)
164171
expect(deletedNode).toBe(null)
172+
expect(linkedList.length).toBe(3)
165173
expect((linkedList.head as Node).value).toBe(1)
166174
expect(((linkedList.head as Node).next as Node).value).toBe(2)
167175
expect((linkedList.tail as Node).value).toBe(3)
@@ -176,6 +184,7 @@ describe('LinkedList class', () => {
176184

177185
const deletedNode = linkedList.delete(3)
178186
expect((deletedNode as Node).value).toBe(3)
187+
expect(linkedList.length).toBe(2)
179188
expect((linkedList.head as Node).value).toBe(1)
180189
expect(((linkedList.head as Node).next as Node).value).toBe(2)
181190
expect((linkedList.tail as Node).value).toBe(2)
@@ -184,6 +193,7 @@ describe('LinkedList class', () => {
184193
linkedList.append(3)
185194

186195
const deletedNode2 = linkedList.delete(1)
196+
expect(linkedList.length).toBe(2)
187197
expect((deletedNode2 as Node).value).toBe(1)
188198
expect((linkedList.head as Node).value).toBe(2)
189199
expect((linkedList.tail as Node).value).toBe(3)
@@ -194,6 +204,7 @@ describe('LinkedList class', () => {
194204

195205
expect(linkedList.head).toBe(null)
196206
expect(linkedList.tail).toBe(null)
207+
expect(linkedList.length).toBe(0)
197208
})
198209
})
199210

@@ -207,7 +218,7 @@ describe('LinkedList class', () => {
207218
.append(5)
208219
expect((linkedList.deleteHead() as Node).value).toBe(1)
209220
expect(linkedList.toString()).toBe('2,3,4,5')
210-
221+
expect(linkedList.length).toBe(4)
211222
expect((linkedList.head as Node).value).toBe(2)
212223
expect((linkedList.tail as Node).value).toBe(5)
213224
})
@@ -228,6 +239,7 @@ describe('LinkedList class', () => {
228239
it('should delete tail node and handle head & tail correctly', () => {
229240
linkedList.append(1).deleteTail()
230241
expect(linkedList.toString()).toBe('')
242+
expect(linkedList.length).toBe(0)
231243
expect(linkedList.head).toBe(null)
232244
expect(linkedList.tail).toBe(null)
233245

@@ -238,16 +250,15 @@ describe('LinkedList class', () => {
238250
.append(4)
239251
.append(5)
240252
linkedList.deleteTail()
253+
expect(linkedList.length).toBe(4)
241254
expect(linkedList.toString()).toBe('1,2,3,4')
242255
expect((linkedList.head as Node).value).toBe(1)
243256
expect((linkedList.tail as Node).value).toBe(4)
244257
})
245258

246259
it('should return null when list is empty', () => {
247260
expect(linkedList.deleteTail()).toBe(null)
248-
linkedList.append(1)
249-
linkedList.append(2)
250-
expect((linkedList.deleteHead() as Node).value).toBe(1)
261+
expect(linkedList.length).toBe(0)
251262
})
252263
})
253264

@@ -279,6 +290,7 @@ describe('LinkedList class', () => {
279290
linkedList.insert(5, 1)
280291

281292
expect(linkedList.toString()).toBe('1,5,2,1,3')
293+
expect(linkedList.length).toBe(5)
282294
expect((linkedList.head as Node).value).toBe(1)
283295
expect((linkedList.tail as Node).value).toBe(3)
284296
})
@@ -291,6 +303,7 @@ describe('LinkedList class', () => {
291303
.append(3)
292304
linkedList.insert(5, 3)
293305

306+
expect(linkedList.length).toBe(5)
294307
expect(linkedList.toString()).toBe('1,2,1,3,5')
295308
expect((linkedList.head as Node).value).toBe(1)
296309
expect((linkedList.tail as Node).value).toBe(5)
@@ -304,6 +317,7 @@ describe('LinkedList class', () => {
304317
.append(3)
305318
linkedList.insert(5, 1, 'before')
306319

320+
expect(linkedList.length).toBe(5)
307321
expect(linkedList.toString()).toBe('5,1,2,1,3')
308322
expect((linkedList.head as Node).value).toBe(5)
309323
expect((linkedList.tail as Node).value).toBe(3)
@@ -317,6 +331,7 @@ describe('LinkedList class', () => {
317331
.append(3)
318332
linkedList.insert(5, 3, 'before')
319333

334+
expect(linkedList.length).toBe(5)
320335
expect(linkedList.toString()).toBe('1,2,1,5,3')
321336
expect((linkedList.head as Node).value).toBe(1)
322337
expect((linkedList.tail as Node).value).toBe(3)
@@ -329,6 +344,7 @@ describe('LinkedList class', () => {
329344
.append(3)
330345
linkedList.insert(5, 4, 'before')
331346

347+
expect(linkedList.length).toBe(3)
332348
expect(linkedList.toString()).toBe('1,2,3')
333349
})
334350

@@ -337,11 +353,13 @@ describe('LinkedList class', () => {
337353
expect(linkedList.toString()).toBe('')
338354
expect(linkedList.head).toBe(null)
339355
expect(linkedList.tail).toBe(null)
356+
expect(linkedList.length).toBe(0)
340357

341358
linkedList.insert(5, 3, 'before')
342359
expect(linkedList.toString()).toBe('')
343360
expect(linkedList.head).toBe(null)
344361
expect(linkedList.tail).toBe(null)
362+
expect(linkedList.length).toBe(0)
345363
})
346364
})
347365

@@ -354,6 +372,7 @@ describe('LinkedList class', () => {
354372
const reversaledList = linkedList.reversal()
355373
expect(reversaledList).toBeDefined()
356374
expect(reversaledList).toBeInstanceOf(LinkedList)
375+
expect(linkedList.length).toBe(3)
357376
expect(reversaledList.toString()).toBe('3,2,1')
358377
expect((reversaledList.head as Node).value).toBe(3)
359378
expect((reversaledList.tail as Node).value).toBe(1)
@@ -366,6 +385,7 @@ describe('LinkedList class', () => {
366385
expect(reversaledList.toString()).toBe('')
367386
expect(reversaledList.head).toBe(null)
368387
expect(reversaledList.tail).toBe(null)
388+
expect(linkedList.length).toBe(0)
369389
})
370390
})
371391
})

src/linked-list/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,16 @@ const stringifyDefault = (value: any) => {
77
export class LinkedList {
88
public head: Node | null
99
public tail: Node | null
10+
private _length: number
1011

1112
constructor() {
1213
this.head = null
1314
this.tail = null
15+
this._length = 0
16+
}
17+
18+
public get length() {
19+
return this._length
1420
}
1521

1622
/**
@@ -19,6 +25,8 @@ export class LinkedList {
1925
* @returns {LinkedList} 当前链表
2026
*/
2127
public append(value: any): LinkedList {
28+
this._length++
29+
2230
const newNode = new Node(value)
2331

2432
if (!this.head) {
@@ -42,6 +50,8 @@ export class LinkedList {
4250
* @returns {LinkedList} 当前链表
4351
*/
4452
public prepend(value: any): LinkedList {
53+
this._length++
54+
4555
const newNode = new Node(value)
4656

4757
if (!this.head) {
@@ -88,6 +98,7 @@ export class LinkedList {
8898
}
8999

90100
if (currentNode) {
101+
this._length++
91102
const newNode = new Node(value, currentNode.next)
92103
if (currentNode === this.tail) {
93104
this.tail = newNode
@@ -99,6 +110,7 @@ export class LinkedList {
99110

100111
if (currentNode) {
101112
if (targetValue === currentNode.value) {
113+
this._length++
102114
const newNode = new Node(value, this.head)
103115
this.head = newNode
104116
} else {
@@ -113,6 +125,7 @@ export class LinkedList {
113125
}
114126

115127
if (prevNode) {
128+
this._length++
116129
const newNode = new Node(value, prevNode.next)
117130
prevNode.next = newNode
118131
}
@@ -139,6 +152,7 @@ export class LinkedList {
139152
// 查看head是不是需要删除的node
140153
while (this.head && this.head.value === value) {
141154
deletedNode = this.head
155+
this._length--
142156
this.head = this.head.next
143157
}
144158

@@ -148,6 +162,7 @@ export class LinkedList {
148162
while (currentNode !== null && currentNode.next) {
149163
if (currentNode.next.value === value) {
150164
deletedNode = currentNode.next
165+
this._length--
151166
currentNode.next = currentNode.next.next
152167
} else {
153168
currentNode = currentNode.next
@@ -171,6 +186,7 @@ export class LinkedList {
171186
const headNode = this.head
172187

173188
if (headNode !== null) {
189+
this._length--
174190
deletedNode = headNode
175191
if (this.head === this.tail) {
176192
this.head = null
@@ -193,13 +209,15 @@ export class LinkedList {
193209
if (this.head !== null) {
194210
if (this.head === this.tail) {
195211
deletedNode = this.head
212+
this._length--
196213
this.head = null
197214
this.tail = null
198215
} else {
199216
let currentNode = this.head
200217
while (currentNode.next !== null) {
201218
if (currentNode.next.next === null) {
202219
deletedNode = currentNode.next.next
220+
this._length--
203221
currentNode.next = null
204222
} else {
205223
currentNode = currentNode.next

0 commit comments

Comments
 (0)