-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubly-linked-list.js
80 lines (71 loc) · 1.63 KB
/
doubly-linked-list.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
class Node {
constructor(prev, data, next) {
this.prev = prev;
this.data = data;
this.next = next;
}
}
class DoublyLinkedList {
#head;
#tail;
constructor() {
this.#head = null;
this.#tail = null;
}
prepend(data) {
const newNode = new Node(null, data, this.#head);
if (this.#head) {
this.#head.prev = newNode;
}
this.#head = newNode;
this.#tail = this.#tail || newNode;
}
append(data) {
const newNode = new Node(this.#tail, data, null);
if (this.#tail) {
this.#tail.next = newNode;
}
this.#tail = newNode;
this.#head = this.#head || newNode;
}
find(item) {
let currentNode = this.#tail;
while (currentNode) {
if (currentNode.data == item) {
return currentNode;
}
currentNode = currentNode.prev;
}
return null;
}
insert(newData, item) {
const nextElement = this.find(item);
if (!nextElement) return;
const newNode = new Node(nextElement?.prev, newData, nextElement);
nextElement.prev.next = newNode;
nextElement.prev = newNode;
}
remove(item) {
const element = this.find(item);
if (!element) return;
if (element.prev) element.prev.next = element.next;
if (element.next) element.next.prev = element.prev;
element.next = null
element.prev = null
}
display() {
let currentNode = this.#head;
while (currentNode) {
console.log("display: ", currentNode.data);
currentNode = currentNode.next;
}
}
}
const list = new DoublyLinkedList();
list.prepend(2);
list.prepend(3);
list.append(4);
list.append(5);
list.insert(8, 4);
list.remove(5);
list.display();