-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLinkedList.js
237 lines (199 loc) · 4.73 KB
/
DoublyLinkedList.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
class Node {
constructor (element) {
this.element = element
this.next = null
this.prev = null
}
}
class DoubleLinkedList {
constructor () {
this.length = 0
this.head = null
this.tail = null
}
// Add new element
append (element) {
const node = new Node(element)
if (!this.head) {
this.head = node
this.tail = node
} else {
node.prev = this.tail
this.tail.next = node
this.tail = node
}
this.length++
}
// Add element
insert (position, element) {
// Check of out-of-bound values
if (position >= 0 && position <= this.length) {
const node = new Node(element)
let current = this.head
let previous = 0
let index = 0
if (position === 0) {
if (!this.head) {
this.head = node
this.tail = node
} else {
node.next = current
current.prev = node
this.head = node
}
} else if (position === this.length) {
current = this.tail
current.next = node
node.prev = current
this.tail = node
} else {
while (index++ < position) {
previous = current
current = current.next
}
node.next = current
previous.next = node
// New
current.prev = node
node.prev = previous
}
this.length++
return true
} else {
return false
}
}
// Remove element at any position
removeAt (position) {
// look for out-of-bounds value
if (position > -1 && position < this.length) {
let current = this.head
let previous = 0
let index = 0
// Removing first item
if (position === 0) {
this.head = current.next
// if there is only one item, update this.tail //NEW
if (this.length === 1) {
this.tail = null
} else {
this.head.prev = null
}
} else if (position === this.length - 1) {
current = this.tail
this.tail = current.prev
this.tail.next = null
} else {
while (index++ < position) {
previous = current
current = current.next
}
// link previous with current's next - skip it
previous.next = current.next
current.next.prev = previous
}
this.length--
return current.element
} else {
return null
}
}
// Get the indexOf item
indexOf (elm) {
let current = this.head
let index = -1
// If element found then return its position
while (current) {
if (elm === current.element) {
return ++index
}
index++
current = current.next
}
// Else return -1
return -1
}
// Find the item in the list
isPresent (elm) {
return this.indexOf(elm) !== -1
}
// Delete an item from the list
delete (elm) {
return this.removeAt(this.indexOf(elm))
}
// Delete first item from the list
deleteHead () {
this.removeAt(0)
}
// Delete last item from the list
deleteTail () {
this.removeAt(this.length - 1)
}
// Print item of the string
toString () {
let current = this.head
let string = ''
while (current) {
string += current.element + (current.next ? '\n' : '')
current = current.next
}
return string
}
// Convert list to array
toArray () {
const arr = []
let current = this.head
while (current) {
arr.push(current.element)
current = current.next
}
return arr
}
// Check if list is empty
isEmpty () {
return this.length === 0
}
// Get the size of the list
size () {
return this.length
}
// Get the this.head
getHead () {
return this.head
}
// Get the this.tail
getTail () {
return this.tail
}
// Method to iterate over the LinkedList
iterator () {
let currentNode = this.getHead()
if (currentNode === null) return -1
const iterate = function * () {
while (currentNode) {
yield currentNode.element
currentNode = currentNode.next
}
}
return iterate()
}
// Method to log the LinkedList, for debugging
// it' a circular structure, so can't use stringify to debug the whole structure
log () {
let currentNode = this.getHead()
while (currentNode) {
console.log(currentNode.element)
currentNode = currentNode.next
}
}
}
// Example
// const newDoubleLinkedList = new DoubleLinkedList()
// newDoubleLinkedList.append(1)
// newDoubleLinkedList.append(2)
// newDoubleLinkedList.size() // returns 2
// const iterate = newDoubleLinkedList.iterator()
// console.log(iterate.next().value) // 1
// console.log(iterate.next().value) // 2
// console.log(newDoubleLinkedList.log())
export { DoubleLinkedList }