-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly-linked-list.js
106 lines (93 loc) · 2.43 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
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
const DoublyListNode = require("./doubly-list-node");
function DoublyLinkedList() {
this.size = 0;
this.head = new DoublyListNode(0);
this.tail = new DoublyListNode(0);
this.head.next = this.tail;
this.tail.prev = this.head; // head ==== tail
}
// Add at index
DoublyLinkedList.prototype.addAtIndex = function(index, value) {
const { size } = this;
if (index < 0 || index > size) {
return;
}
let prev = this.head;
let next = this.tail;
if (index < size - index) {
while (index) {
prev = prev.next;
index--;
}
next = prev.next;
} else {
while(size - index) {
next = next.prev;
index++;
}
prev = next.prev;
}
const newNode = new DoublyListNode(value);
newNode.prev = prev;
newNode.next = next;
prev.next = newNode;
next.prev = newNode;
this.size++;
}
DoublyLinkedList.prototype.addAtHead = function(value) {
return this.addAtIndex(0, value);
}
DoublyLinkedList.prototype.getAtIndex = function(index) {
const { size } = this;
if (index < 0 || index >= size) {
return null;
}
if (index < size - index) {
let node = this.head;
while (index >= 0) {
node = node.next;
index--;
}
return node;
} else {
let node = this.tail;
while(size - index) {
node = node.prev;
index++;
}
return node;
}
}
DoublyLinkedList.prototype.deleteAtIndex = function(index) {
const { size } = this;
if (index < 0 || index >= size) {
return null;
}
let nodeToDelete = this.head;
if (index < size - index) {
while (index >= 0) {
nodeToDelete = nodeToDelete.next;
index--;
}
} else {
nodeToDelete = this.tail;
while(size - index) {
nodeToDelete = nodeToDelete.prev;
index++;
}
}
console.log('NODE TO DELETE>>', nodeToDelete);
const prevNode = nodeToDelete.prev;
const nextNode = nodeToDelete.next;
prevNode.next = nextNode;
nextNode.prev = prevNode;
nodeToDelete.prev = null;
nodeToDelete.next = null;
this.size--;
}
module.exports = DoublyLinkedList
// ind 0, size 3, size - index = 3
// ind 3, size 3, size - index = 0
// ind 1, size 3, size - index = 2
// ind 2, size 3, size - index = 1
// head ==== 3 ==== 4 === 5 === tail