Skip to content

Commit be15d08

Browse files
authored
merge: replaced constructor function with es6 class syntax (#900)
* replaced constructor function with es6 class syntax * formatted code with standard.js
1 parent 8bf29fe commit be15d08

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

Data-Structures/Linked-List/DoublyLinkedList.js

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,57 @@
1-
// Hamza chabchoub contribution for a university project
2-
function DoubleLinkedList () {
3-
const Node = function (element) {
1+
class Node {
2+
constructor (element) {
43
this.element = element
54
this.next = null
65
this.prev = null
76
}
7+
}
88

9-
let length = 0
10-
let head = null
11-
let tail = null
9+
class DoubleLinkedList {
10+
constructor () {
11+
this.length = 0
12+
this.head = null
13+
this.tail = null
14+
}
1215

1316
// Add new element
14-
this.append = function (element) {
17+
append (element) {
1518
const node = new Node(element)
1619

17-
if (!head) {
18-
head = node
19-
tail = node
20+
if (!this.head) {
21+
this.head = node
22+
this.tail = node
2023
} else {
21-
node.prev = tail
22-
tail.next = node
23-
tail = node
24+
node.prev = this.tail
25+
this.tail.next = node
26+
this.tail = node
2427
}
2528

26-
length++
29+
this.length++
2730
}
2831

2932
// Add element
30-
this.insert = function (position, element) {
33+
insert (position, element) {
3134
// Check of out-of-bound values
32-
if (position >= 0 && position <= length) {
35+
if (position >= 0 && position <= this.length) {
3336
const node = new Node(element)
34-
let current = head
37+
let current = this.head
3538
let previous = 0
3639
let index = 0
3740

3841
if (position === 0) {
39-
if (!head) {
40-
head = node
41-
tail = node
42+
if (!this.head) {
43+
this.head = node
44+
this.tail = node
4245
} else {
4346
node.next = current
4447
current.prev = node
45-
head = node
48+
this.head = node
4649
}
47-
} else if (position === length) {
48-
current = tail
50+
} else if (position === this.length) {
51+
current = this.tail
4952
current.next = node
5053
node.prev = current
51-
tail = node
54+
this.tail = node
5255
} else {
5356
while (index++ < position) {
5457
previous = current
@@ -63,35 +66,35 @@ function DoubleLinkedList () {
6366
node.prev = previous
6467
}
6568

66-
length++
69+
this.length++
6770
return true
6871
} else {
6972
return false
7073
}
7174
}
7275

7376
// Remove element at any position
74-
this.removeAt = function (position) {
77+
removeAt (position) {
7578
// look for out-of-bounds value
76-
if (position > -1 && position < length) {
77-
let current = head
79+
if (position > -1 && position < this.length) {
80+
let current = this.head
7881
let previous = 0
7982
let index = 0
8083

8184
// Removing first item
8285
if (position === 0) {
83-
head = current.next
86+
this.head = current.next
8487

85-
// if there is only one item, update tail //NEW
86-
if (length === 1) {
87-
tail = null
88+
// if there is only one item, update this.tail //NEW
89+
if (this.length === 1) {
90+
this.tail = null
8891
} else {
89-
head.prev = null
92+
this.head.prev = null
9093
}
91-
} else if (position === length - 1) {
92-
current = tail
93-
tail = current.prev
94-
tail.next = null
94+
} else if (position === this.length - 1) {
95+
current = this.tail
96+
this.tail = current.prev
97+
this.tail.next = null
9598
} else {
9699
while (index++ < position) {
97100
previous = current
@@ -103,16 +106,16 @@ function DoubleLinkedList () {
103106
current.next.prev = previous
104107
}
105108

106-
length--
109+
this.length--
107110
return current.element
108111
} else {
109112
return null
110113
}
111114
}
112115

113116
// Get the indexOf item
114-
this.indexOf = function (elm) {
115-
let current = head
117+
indexOf (elm) {
118+
let current = this.head
116119
let index = -1
117120

118121
// If element found then return its position
@@ -130,28 +133,28 @@ function DoubleLinkedList () {
130133
}
131134

132135
// Find the item in the list
133-
this.isPresent = (elm) => {
136+
isPresent (elm) {
134137
return this.indexOf(elm) !== -1
135138
}
136139

137140
// Delete an item from the list
138-
this.delete = (elm) => {
141+
delete (elm) {
139142
return this.removeAt(this.indexOf(elm))
140143
}
141144

142145
// Delete first item from the list
143-
this.deleteHead = function () {
146+
deleteHead () {
144147
this.removeAt(0)
145148
}
146149

147150
// Delete last item from the list
148-
this.deleteTail = function () {
149-
this.removeAt(length - 1)
151+
deleteTail () {
152+
this.removeAt(this.length - 1)
150153
}
151154

152155
// Print item of the string
153-
this.toString = function () {
154-
let current = head
156+
toString () {
157+
let current = this.head
155158
let string = ''
156159

157160
while (current) {
@@ -163,9 +166,9 @@ function DoubleLinkedList () {
163166
}
164167

165168
// Convert list to array
166-
this.toArray = function () {
169+
toArray () {
167170
const arr = []
168-
let current = head
171+
let current = this.head
169172

170173
while (current) {
171174
arr.push(current.element)
@@ -176,27 +179,27 @@ function DoubleLinkedList () {
176179
}
177180

178181
// Check if list is empty
179-
this.isEmpty = function () {
180-
return length === 0
182+
isEmpty () {
183+
return this.length === 0
181184
}
182185

183186
// Get the size of the list
184-
this.size = function () {
185-
return length
187+
size () {
188+
return this.length
186189
}
187190

188-
// Get the head
189-
this.getHead = function () {
190-
return head
191+
// Get the this.head
192+
getHead () {
193+
return this.head
191194
}
192195

193-
// Get the tail
194-
this.getTail = function () {
195-
return tail
196+
// Get the this.tail
197+
getTail () {
198+
return this.tail
196199
}
197200

198201
// Method to iterate over the LinkedList
199-
this.iterator = function () {
202+
iterator () {
200203
let currentNode = this.getHead()
201204
if (currentNode === null) return -1
202205

@@ -211,7 +214,7 @@ function DoubleLinkedList () {
211214

212215
// Method to log the LinkedList, for debugging
213216
// it' a circular structure, so can't use stringify to debug the whole structure
214-
this.log = function () {
217+
log () {
215218
let currentNode = this.getHead()
216219
while (currentNode) {
217220
console.log(currentNode.element)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy