-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-mutable.js
More file actions
120 lines (105 loc) · 2.6 KB
/
Copy path5-mutable.js
File metadata and controls
120 lines (105 loc) · 2.6 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
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
'use strict';
class List {
constructor() {
this.head = null;
this.length = 0;
}
prepend(value) {
this.head = { value, next: this.head };
this.length++;
return this;
}
append(value) {
const node = { value, next: null };
if (this.head === null) {
this.head = node;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
}
this.length++;
return this;
}
insert(index, value) {
if (index <= 0) return this.prepend(value);
if (index >= this.length) return this.append(value);
let current = this.head;
let i = 0;
while (i < index - 1) {
current = current.next;
i++;
}
current.next = { value, next: current.next };
this.length++;
return this;
}
delete(index) {
if (this.head === null) throw new Error('List is empty');
if (index < 0 || index >= this.length) {
throw new Error('Index out of bounds');
}
if (index === 0) {
this.head = this.head.next;
this.length--;
return this;
}
let current = this.head;
let i = 0;
while (i < index - 1) {
current = current.next;
i++;
}
current.next = current.next.next;
this.length--;
return this;
}
at(index) {
let current = this.head;
let i = 0;
while (current !== null && i < index) {
current = current.next;
i++;
}
return current ? current.value : undefined;
}
toArray() {
const result = new Array(this.length);
let current = this.head;
let i = 0;
while (current !== null) {
result[i++] = current.value;
current = current.next;
}
return result;
}
*[Symbol.iterator]() {
let current = this.head;
while (current !== null) {
yield current.value;
current = current.next;
}
}
}
// Usage
const list = new List();
list.prepend(1);
list.prepend(2);
list.prepend(3);
// list ──▶ List { length: 3 }
// │
// ▼ head
// ┌───┐ ┌───┐ ┌───┐
// │ 3 │───▶│ 2 │───▶│ 1 │───▶ null
// └───┘ └───┘ └───┘
console.log(`list.toArray() ->`, list.toArray());
console.log(`list.at(1) ->`, list.at(1));
console.log(`[...list] ->`, [...list]);
list.append(0);
console.log(`after append(0) ->`, list.toArray());
list.insert(1, 4);
console.log(`after insert(1, 4) ->`, list.toArray());
list.delete(2);
console.log(`after delete(2) ->`, list.toArray());