-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoubly-linked-list.js
More file actions
222 lines (177 loc) · 4.47 KB
/
doubly-linked-list.js
File metadata and controls
222 lines (177 loc) · 4.47 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
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
class DLLNode {
constructor(val) {
this.val = val;
this.prev = null;
this.next = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = this.tail = null;
this.length = 0;
}
push(val) {
if (!this.head) {
this.head = this.tail = new DLLNode(val);
} else {
const newLastNode = new DLLNode(val);
newLastNode.prev = this.tail;
this.tail.next = newLastNode;
this.tail = newLastNode;
}
this.length++;
return this;
}
unshift(val) {
if (!this.head) {
this.head = this.tail = new DLLNode(val);
} else {
const newFirstNode = new DLLNode(val);
newFirstNode.next = this.head;
this.head.prev = newFirstNode;
this.head = newFirstNode;
}
this.length++;
return this;
}
pop() {
if (!this.head) {
return -1;
} else if (!this.head.next) {
const removedNode = this.head;
this.head = this.tail = null;
this.length--;
return removedNode;
}
const lastNode = this.tail;
const secondToLast = lastNode.prev;
secondToLast.next = null;
this.tail = secondToLast;
lastNode.next = lastNode.prev = null;
this.length--;
return lastNode;
}
shift() {
if (!this.head) {
return -1;
} else if (!this.head.next) {
const loneNode = this.head;
this.head = this.tail = null;
this.length--;
return loneNode;
}
const removedHead = this.head;
this.head = this.head.next;
this.head.prev = null;
this.length--;
removedHead.prev = removedHead.next = null;
return removedHead;
}
get(idx) {
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
if (idx < 0 || idx >= this.length) {
return -1;
}
if (!this.head) {
return -1;
} else if (!this.head.next) {
return this.head;
}
// start at first position (head / 0)
if (idx <= this.length / 2) {
let stepsLeft = idx;
let trav = this.head;
while (stepsLeft !== 0) {
trav = trav.next;
stepsLeft--;
}
return trav;
// start at last position (tail / this.length - 1)
} else if (idx > this.length / 2) {
let stepsLeft = this.length - 1;
let trav = this.tail;
while (stepsLeft !== idx) {
trav = trav.prev;
stepsLeft--;
}
return trav;
}
}
set(idx, val) {
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
if (idx < 0 || idx >= this.length) {
return -1;
}
if (!this.head) {
return -1;
} else if (!this.head.next) {
this.head.val = val;
return this;
} else {
if (idx <= this.length / 2) {
let trav = this.head;
let stepsLeft = idx;
while (stepsLeft !== 0) {
trav = trav.next;
stepsLeft--;
}
trav.val = val;
return this;
} else if (idx > this.length / 2) {
let trav = this.tail;
let stepsLeft = this.length - 1;
while (stepsLeft !== idx) {
trav = trav.prev;
stepsLeft--;
}
trav.val = val;
return this;
}
}
}
insert(idx, val) {
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
if (idx === 0) return this.unshift(val);
if (idx < 0 || idx > this.length) return -1;
if (idx === this.length) return this.push(val);
const beforeNode = this.get(idx - 1);
const afterNode = beforeNode.next;
const newNode = new DLLNode(val);
// restore forward link
newNode.next = afterNode, afterNode.prev = newNode;
// create new link between beforeNode <--> newNode
beforeNode.next = newNode, newNode.prev = beforeNode;
if (idx === this.length - 1) {
this.tail = newNode.next;
}
this.length++;
return this;
}
remove(idx) {
if (!Number.isSafeInteger(idx)) {
throw new Error('Invalid index');
}
if (idx === 0) return this.shift();
if (idx < 0 || idx >= this.length) return -1;
if (idx === this.length - 1) return this.pop();
if (!this.head.next) {
}
const beforeNode = this.get(idx - 1);
const removedNode = beforeNode.next;
const afterNode = beforeNode.next.next;
beforeNode.next = afterNode, afterNode.prev = beforeNode;
removedNode.next = null, removedNode.prev = null;
this.length--;
return this;
}
}
module.exports = {
DLLNode,
DoublyLinkedList,
}