forked from AllAlgorithms/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublyLinkedList.js
More file actions
226 lines (214 loc) · 5.16 KB
/
doublyLinkedList.js
File metadata and controls
226 lines (214 loc) · 5.16 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
/**
* @author Rashik Ansar
*
* Implementation of doubly linked list
* Doubly linked list is a linear data structure
*/
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0;
}
/**
* Adding new node as a tail of the linked list
* @param {any} data This dataue is added at the end of list
* @returns {DoublyLinkedList} LinkedList after adding new node as tail
*/
push(data) {
let temp = new Node(data);
if (!this.head) {
this.head = temp;
this.tail = temp;
} else {
this.tail.next = temp;
temp.prev = this.tail;
this.tail = temp;
}
this.length++;
return this;
}
/**
* Adding new node as a head of the linked list
* @param {any} data This dataue is added at the beginning of the list
* @returns {DoublyLinkedList} LinkedList after adding a new node as head
*/
unshift(data) {
let current = new Node(data);
if (!this.head) {
this.head = current;
this.tail = current;
} else {
this.head.prev = current;
current.next = this.head;
this.head = current;
}
this.length++;
return this;
}
/**
* Adding a node to the linkedList at specified position
* @param {number} index Position at which new node to insert
* @param {any} data dataue in the new node
* @returns {DoublyLinkedList} LinkedList after inserting a new node
*/
insert(index, data) {
if (index < 0 || index > this.length) {
throw Error('Given index is out of range');
}
if (index === this.length) {
return this.push(data);
}
if (index === 0) {
return this.unshift(data);
}
let insertNode = new Node(data);
let previous = this.get(index - 1);
let temp = previous.next;
previous.next = insertNode;
insertNode.prev = previous;
insertNode.next = temp;
temp.prev = insertNode;
this.length++;
return this;
}
/**
* Removes the node at the end of linked list(tail of linked list)
* @returns {Node} the node which is going to pop
*/
pop() {
if (!this.head) {
throw Error(
'UNDERFLOW :::: LinkedList is empty, there is nothing to remove'
);
}
let temp = this.tail;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.tail = temp.prev;
this.tail.next = null;
temp.prev = null;
}
this.length--;
return temp;
}
/**
* Removes the node from the beginnig of linked list(head of linked list)
* @returns {Node} the node which is going to shift
*/
shift() {
if (!this.head) {
throw Error(
'UNDERFLOW :::: LinkedList is empty, there is nothing to remove'
);
}
let current = this.head;
if (this.length === 1) {
this.head = null;
this.tail = null;
} else {
this.head = current.next;
this.head.prev = null;
current.next = null;
}
this.length--;
return current;
}
/**
* Removes a node from the linkedList at specified position
* @param {number} index
* @returns {Node} Node which is removed from LinkedList
*/
remove(index) {
if (index < 0 || index > this.length) {
throw Error('Given index is out of range');
}
if (index === this.length - 1) {
return this.pop();
}
if (index === 0) {
return this.shift();
}
let removeNode = this.get(index);
let before = removeNode.prev;
let after = removeNode.next;
before.next = after;
after.prev = before;
removeNode.next = null;
removeNode.prev = null;
this.length--;
return removeNode;
}
/**
* Retrieve the node at specified index
* @param {number} index Index of the node
* @returns {Node} LinkedList Node at specified index
*/
get(index) {
if (index < 0 || index >= this.length) {
throw Error('Given index is out of range');
}
let current;
if (index <= this.length / 2) {
let counter = 0;
current = this.head;
while (counter !== index) {
current = current.next;
counter++;
}
} else {
let counter = this.length - 1;
current = this.tail;
while (counter !== index) {
current = current.prev;
counter--;
}
}
return current;
}
/**
* Change the data of node at specified index
* @param {number} index Index of the node
* @param {any} data data replaces the current data at given index
* @returns {DoublyLinkedList} LinkedList
*/
set(index, data) {
let existedNode = this.get(index);
if (existedNode) {
existedNode.data = data;
return this;
}
}
/**
* Traversing or Printing the Linked list
*/
traverse() {
let current = this.head;
console.log(this.length);
while (current) {
console.log(current.data);
current = current.next;
}
}
/**
* @returns {[]} Linkedlist data as elements in Array
*/
listAsArray() {
let arr = [];
let current = this.head;
while (current) {
arr.push(current.data);
current = current.next;
}
return arr;
}
}