-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdoublyLinkedList.js
More file actions
245 lines (222 loc) · 6.31 KB
/
doublyLinkedList.js
File metadata and controls
245 lines (222 loc) · 6.31 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const Table = require('cli-table2');
const chalk = require('chalk');
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
}
append(item) {
let node = new Node(item);
if (!this.head) {
this.head = node;
this.tail = node;
} else {
node.prev = this.tail;
this.tail.next = node;
this.tail = node
}
}
appendAt(pos, item) {
let current = this.head;
let counter = 1;
let node = new Node(item);
if (pos == 0) {
this.head.prev = node
node.next = this.head
this.head = node
} else {
while (current) {
current = current.next;
if (counter == pos) {
node.prev = current.prev
current.prev.next = node
node.next = current
current.prev = node
}
counter++
}
}
}
appendAfter(item) {
//
}
remove(item) {
let current = this.head;
while (current) {
if (current.data === item) {
if (current == this.head && current == this.tail) {
this.head = null;
this.tail = null;
} else if (current == this.head) {
this.head = this.head.next
this.head.prev = null
} else if (current == this.tail) {
this.tail = this.tail.prev;
this.tail.next = null;
} else {
current.prev.next = current.next;
current.next.prev = current.prev;
}
}
current = current.next
}
}
removeAt(pos) {
let current = this.head;
let counter = 1;
if (pos == 0) {
this.head = this.head.next;
this.head.prev = null;
} else {
while (current) {
current = current.next
if (current == this.tail) {
this.tail = this.tail.prev;
this.tail.next = null;
} else if (counter == pos) {
current.prev.next = current.next;
current.next.prev = current.prev;
break;
}
counter++;
}
}
}
reverse() {
let current = this.head;
let prev = null;
while (current) {
let next = current.next
current.next = prev
current.prev = next
prev = current
current = next
}
this.tail = this.head
this.head = prev
}
swap(nodeOne, nodeTwo) {
let current = this.head;
let counter = 0;
let firstNode;
// Make sure we are okay to go
if (nodeOne === nodeTwo) {
console.log("ERROR: 'SWAP' both the nodes must be different!");
return false;
} else if (nodeOne > nodeTwo) {
let temp = nodeOne;
nodeOne = nodeTwo;
nodeTwo = temp;
}
if (nodeOne < 0 || nodeTwo < 0) {
console.log("ERROR: 'SWAP' both the nodes must be index & index can not be negative!");
return false;
}
// Swap nodes
while (current !== null) {
if (counter == nodeOne) {
firstNode = current;
} else if (counter == nodeTwo) {
let temp = current.data;
current.data = firstNode.data;
firstNode.data = temp;
}
current = current.next;
counter++;
}
return true
}
length() {
let current = this.head;
let counter = 0;
while (current !== null) {
counter++
current = current.next
}
return counter;
}
display() {
let current = this.head;
let elements = [];
while (current !== null) {
elements.push(current.data);
current = current.next
}
return elements.join(" ");
}
isEmpty() {
return this.length() < 1
}
traverse(fn) {
if (!fn || typeof fn !== 'function') {
console.log("ERROR: 'TRAVERSE' function is undefined!");
return false;
}
let current = this.head;
while (current !== null) {
fn(current)
current = current.next;
}
return true;
}
traverseReverse(fn) {
if (!fn || typeof fn !== 'function') {
console.log("ERROR: 'TRAVERSE_REVERSE' function is undefined!");
return false;
}
let current = this.tail;
while (current !== null) {
fn(current)
current = current.prev;
}
return true;
}
search(item) {
let current = this.head;
let counter = 0;
while (current) {
if (current.data == item) {
return counter
}
current = current.next
counter++
}
return false;
}
/*
* EXTRA
*/
prettyPrint() {
let current = this.head;
let output = [chalk.red('Head') + ' ->'];
while (current !== null) {
output.push(chalk.cyan(current.data));
current = current.next;
}
output.push('<- ' + chalk.red('Tail'));
this.table(output);
return true;
} // </ PrettyPrint>
table(output) {
let table = new Table({
chars: {
'top': '─', 'top-mid': '╤', 'top-left': '╔', 'top-right': '╗'
, 'bottom': '─', 'bottom-mid': '╧', 'bottom-left': '╚', 'bottom-right': '╝'
, 'left': '│', 'left-mid': '╟', 'mid': '─', 'mid-mid': '┼'
, 'right': '│', 'right-mid': '╢', 'middle': '│'
},
style: { border: ['yellow'] }
});
table.push(output);
console.log(table.toString());
return true
} // </ Table>
}
module.exports = DoublyLinkedList