|
1 | | -// Hamza chabchoub contribution for a university project |
2 | | -function DoubleLinkedList () { |
3 | | - const Node = function (element) { |
| 1 | +class Node { |
| 2 | + constructor (element) { |
4 | 3 | this.element = element |
5 | 4 | this.next = null |
6 | 5 | this.prev = null |
7 | 6 | } |
| 7 | +} |
8 | 8 |
|
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 | + } |
12 | 15 |
|
13 | 16 | // Add new element |
14 | | - this.append = function (element) { |
| 17 | + append (element) { |
15 | 18 | const node = new Node(element) |
16 | 19 |
|
17 | | - if (!head) { |
18 | | - head = node |
19 | | - tail = node |
| 20 | + if (!this.head) { |
| 21 | + this.head = node |
| 22 | + this.tail = node |
20 | 23 | } 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 |
24 | 27 | } |
25 | 28 |
|
26 | | - length++ |
| 29 | + this.length++ |
27 | 30 | } |
28 | 31 |
|
29 | 32 | // Add element |
30 | | - this.insert = function (position, element) { |
| 33 | + insert (position, element) { |
31 | 34 | // Check of out-of-bound values |
32 | | - if (position >= 0 && position <= length) { |
| 35 | + if (position >= 0 && position <= this.length) { |
33 | 36 | const node = new Node(element) |
34 | | - let current = head |
| 37 | + let current = this.head |
35 | 38 | let previous = 0 |
36 | 39 | let index = 0 |
37 | 40 |
|
38 | 41 | if (position === 0) { |
39 | | - if (!head) { |
40 | | - head = node |
41 | | - tail = node |
| 42 | + if (!this.head) { |
| 43 | + this.head = node |
| 44 | + this.tail = node |
42 | 45 | } else { |
43 | 46 | node.next = current |
44 | 47 | current.prev = node |
45 | | - head = node |
| 48 | + this.head = node |
46 | 49 | } |
47 | | - } else if (position === length) { |
48 | | - current = tail |
| 50 | + } else if (position === this.length) { |
| 51 | + current = this.tail |
49 | 52 | current.next = node |
50 | 53 | node.prev = current |
51 | | - tail = node |
| 54 | + this.tail = node |
52 | 55 | } else { |
53 | 56 | while (index++ < position) { |
54 | 57 | previous = current |
@@ -63,35 +66,35 @@ function DoubleLinkedList () { |
63 | 66 | node.prev = previous |
64 | 67 | } |
65 | 68 |
|
66 | | - length++ |
| 69 | + this.length++ |
67 | 70 | return true |
68 | 71 | } else { |
69 | 72 | return false |
70 | 73 | } |
71 | 74 | } |
72 | 75 |
|
73 | 76 | // Remove element at any position |
74 | | - this.removeAt = function (position) { |
| 77 | + removeAt (position) { |
75 | 78 | // 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 |
78 | 81 | let previous = 0 |
79 | 82 | let index = 0 |
80 | 83 |
|
81 | 84 | // Removing first item |
82 | 85 | if (position === 0) { |
83 | | - head = current.next |
| 86 | + this.head = current.next |
84 | 87 |
|
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 |
88 | 91 | } else { |
89 | | - head.prev = null |
| 92 | + this.head.prev = null |
90 | 93 | } |
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 |
95 | 98 | } else { |
96 | 99 | while (index++ < position) { |
97 | 100 | previous = current |
@@ -103,16 +106,16 @@ function DoubleLinkedList () { |
103 | 106 | current.next.prev = previous |
104 | 107 | } |
105 | 108 |
|
106 | | - length-- |
| 109 | + this.length-- |
107 | 110 | return current.element |
108 | 111 | } else { |
109 | 112 | return null |
110 | 113 | } |
111 | 114 | } |
112 | 115 |
|
113 | 116 | // Get the indexOf item |
114 | | - this.indexOf = function (elm) { |
115 | | - let current = head |
| 117 | + indexOf (elm) { |
| 118 | + let current = this.head |
116 | 119 | let index = -1 |
117 | 120 |
|
118 | 121 | // If element found then return its position |
@@ -130,28 +133,28 @@ function DoubleLinkedList () { |
130 | 133 | } |
131 | 134 |
|
132 | 135 | // Find the item in the list |
133 | | - this.isPresent = (elm) => { |
| 136 | + isPresent (elm) { |
134 | 137 | return this.indexOf(elm) !== -1 |
135 | 138 | } |
136 | 139 |
|
137 | 140 | // Delete an item from the list |
138 | | - this.delete = (elm) => { |
| 141 | + delete (elm) { |
139 | 142 | return this.removeAt(this.indexOf(elm)) |
140 | 143 | } |
141 | 144 |
|
142 | 145 | // Delete first item from the list |
143 | | - this.deleteHead = function () { |
| 146 | + deleteHead () { |
144 | 147 | this.removeAt(0) |
145 | 148 | } |
146 | 149 |
|
147 | 150 | // Delete last item from the list |
148 | | - this.deleteTail = function () { |
149 | | - this.removeAt(length - 1) |
| 151 | + deleteTail () { |
| 152 | + this.removeAt(this.length - 1) |
150 | 153 | } |
151 | 154 |
|
152 | 155 | // Print item of the string |
153 | | - this.toString = function () { |
154 | | - let current = head |
| 156 | + toString () { |
| 157 | + let current = this.head |
155 | 158 | let string = '' |
156 | 159 |
|
157 | 160 | while (current) { |
@@ -163,9 +166,9 @@ function DoubleLinkedList () { |
163 | 166 | } |
164 | 167 |
|
165 | 168 | // Convert list to array |
166 | | - this.toArray = function () { |
| 169 | + toArray () { |
167 | 170 | const arr = [] |
168 | | - let current = head |
| 171 | + let current = this.head |
169 | 172 |
|
170 | 173 | while (current) { |
171 | 174 | arr.push(current.element) |
@@ -176,27 +179,27 @@ function DoubleLinkedList () { |
176 | 179 | } |
177 | 180 |
|
178 | 181 | // Check if list is empty |
179 | | - this.isEmpty = function () { |
180 | | - return length === 0 |
| 182 | + isEmpty () { |
| 183 | + return this.length === 0 |
181 | 184 | } |
182 | 185 |
|
183 | 186 | // Get the size of the list |
184 | | - this.size = function () { |
185 | | - return length |
| 187 | + size () { |
| 188 | + return this.length |
186 | 189 | } |
187 | 190 |
|
188 | | - // Get the head |
189 | | - this.getHead = function () { |
190 | | - return head |
| 191 | + // Get the this.head |
| 192 | + getHead () { |
| 193 | + return this.head |
191 | 194 | } |
192 | 195 |
|
193 | | - // Get the tail |
194 | | - this.getTail = function () { |
195 | | - return tail |
| 196 | + // Get the this.tail |
| 197 | + getTail () { |
| 198 | + return this.tail |
196 | 199 | } |
197 | 200 |
|
198 | 201 | // Method to iterate over the LinkedList |
199 | | - this.iterator = function () { |
| 202 | + iterator () { |
200 | 203 | let currentNode = this.getHead() |
201 | 204 | if (currentNode === null) return -1 |
202 | 205 |
|
@@ -211,7 +214,7 @@ function DoubleLinkedList () { |
211 | 214 |
|
212 | 215 | // Method to log the LinkedList, for debugging |
213 | 216 | // it' a circular structure, so can't use stringify to debug the whole structure |
214 | | - this.log = function () { |
| 217 | + log () { |
215 | 218 | let currentNode = this.getHead() |
216 | 219 | while (currentNode) { |
217 | 220 | console.log(currentNode.element) |
|