-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path12.DoublyLinkedLists.js
237 lines (188 loc) · 4.74 KB
/
12.DoublyLinkedLists.js
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
// Big O of Doubly Linked Lists
// Insertion - O(1)
// Removal - O(1)
// Searching - O(N)
// Access - O(N)
// Technically searching is O(N / 2), but that's still O(N)
// Doubly Linked Lists are almost identical to Singly Linked Lists except there is an additional pointer to previous nodes
// Better than Singly Linked Lists for finding nodes and can be done in half the time!
// However, they do take up more memory considering the extra pointer
// Doubly linked lists are used to implement other data structures and certain types of caches
class Node {
constructor(value) {
this.value = value
this.next = null
this.prev = null
}
}
class DoublyLinkedList {
constructor() {
this.head = null
this.tail = null
this.length = 0
}
push(value) {
const newNode = new Node(value);
// if list is empty
if(!this.head) {
this.head = this.tail = newNode
this.length++
return this
}
const currentTail = this.tail;
currentTail.next = newNode;
newNode.prev = currentTail
this.tail = newNode
this.length++
return this;
}
pop() {
// if there is no element in the list
if(!this.head) {
return null
}
// if there is only one element in the list
if(!this.head.next) {
const currentTail = this.tail;
this.head = this.tail = null;
this.length--
return currentTail
}
const last = this.tail;
const secondLast = last.prev;
secondLast.next = null;
last.prev = null
this.tail = secondLast;
this.length--
return last;
}
shift() {
// if there is no element in the list
if(!this.head) {
return null
}
// if there is only one element in the list
if(!this.head.next) {
const currentHead = this.head
this.head = this.tail = null
this.length--
return currentHead
}
const currentHead = this.head;
const newHead = currentHead.next;
currentHead.next = null;
newHead.prev = null;
this.head = newHead;
this.length--
return currentHead
}
unshift(value) {
const newNode = new Node(value);
const currentHead = this.head;
// if list was empty
if(!currentHead) {
this.head = this.tail = newNode;
this.length++
return this;
}
newNode.next = currentHead
currentHead.prev = newNode
this.head = newNode
this.length++
return this;
}
get(index) {
if(index < 0 || index >= this.length) {
return null
}
const fromStart = index <= Math.floor(this.length / 2) ? true : false
let current, count;
if(fromStart) {
console.log("Working from the start...!")
current = this.head;
count = 0
while(count < index) { // OR while(count !== index)
current = current.next;
count++
}
} else {
console.log("Working from the end...!")
current = this.tail;
count = this.length - 1
while(count > index) { // OR while(count !== index)
current = current.prev;
count--
}
}
return current
}
set(index, value) {
const foundNode = this.get(index);
if(foundNode) {
foundNode.value = value;
return true
}
return false
}
insert(index, value) {
if(index < 0 || index > this.length) {
return false
}
if(index === this.length) {
return !!this.push(value)
}
if(index === 0) {
return !!this.unshift(value)
}
const newNode = new Node(value);
const previous = this.get(index - 1); // 2
const current = previous.next // 3
previous.next = newNode;
newNode.prev = previous;
newNode.next = current;
current.prev = newNode;
this.length++
return true;
}
remove(index) {
if(index < 0 || index >= this.length) {
return null
}
if(index === this.length - 1) {
return this.pop()
}
if(index === 0) {
return this.shift()
}
const toBeRemoved = this.get(index);
const previous = toBeRemoved.prev;
const next = toBeRemoved.next;
previous.next = next;
next.prev = previous;
// toBeRemoved.prev.next = toBeRemoved.next
// toBeRemoved.next.prev = toBeRemoved.prev
toBeRemoved.next = null;
toBeRemoved.prev = null;
this.length--
return toBeRemoved;
}
}
const l = new DoublyLinkedList();
l.push(1)
l.push(2)
l.push(3)
l.push(4)
// l.push(5)
// l.push(6)
// a = l.unshift("hello")
// let b = l.shift()
// let b = l.pop()
// console.log(b)
// console.log(l.head, l.length)
// console.log(l.insert(1, "hello"))
// console.log(l.get(2))
// console.log(l.get())
// console.log(l.get(2))
console.log(l.remove(1))
// console.log(l.get(9))
console.log(l)