-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
double-linked-list.js
100 lines (81 loc) · 2.03 KB
/
double-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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class DoubleLinkedList extends LinkedList {
tail = null;
createNode(element) {
return { element, next: null, prev: null };
}
push(element) {
const node = this.createNode(element);
if (!this.head) {
this.head = node;
} else {
const current = this.getNodeAt(this.size - 1);
current.next = node;
node.prev = current;
}
this.tail = node;
this.size += 1;
return this.size;
}
insert(element, index = 0) {
if (index > this.size) return false;
const node = this.createNode(element);
if (index === 0) {
if (this.head) {
node.next = this.head;
this.head.prev = node;
} else {
this.tail = node;
}
this.head = node;
} else if (index === this.size) {
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
} else {
const current = this.getNodeAt(index);
const prev = current.prev;
prev.next = node;
current.prev = node;
node.prev = prev;
node.next = current;
}
this.size = this.size + 1;
return true;
};
remove(index = 0) {
if (index > this.size) return null;
let removedNode = this.head;
if (index === 0) {
this.head = removedNode.next;
if (this.size === 1) {
this.tail = null;
} else {
this.head.prev = null;
}
} else if (index === this.size - 1) {
removedNode = this.tail;
this.tail = removedNode.prev;
this.tail.next = null;
} else {
removedNode = this.getNodeAt(index);
const previous = removedNode.prev;
const next = removedNode.next;
previous.next = next;
next.prev = previous;
}
this.size = this.size - 1;
return removedNode;
};
reverse() {
let current = this.head;
this.head = this.tail;
this.tail = current;
for(let i = 0; i<this.size; i++) {
const prev = current.prev;
const next = current.next;
current.prev = next;
current.next = prev;
current = next;
}
}
}