-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.js
More file actions
242 lines (208 loc) · 5.33 KB
/
LinkedList.js
File metadata and controls
242 lines (208 loc) · 5.33 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
/**
* Node
*/
class Node {
constructor(value, prev, next) {
this.value = value;
this.prev = prev || null;
this.next = next || null;
}
}
/**
* Linked List
*/
export class LinkedList {
constructor() {
this.head = this.tail = null; // first & last node
}
/**
* append new node to the end of Linked List
* @param value
*/
appendNode = (value) => {
// if list is empty
if (!this.tail) {
this.head = this.tail = new Node(value);
} else {
const oldTail = this.tail; // save old tail
this.tail = new Node(value); // set new node to the new tail
oldTail.next = this.tail; // set new tail to old tail next
this.tail.prev = oldTail; // set old tail to new tail prev
}
};
/**
* prepend new node to the start of Linked List
* @param value
*/
prependNode = (value) => {
// if list is empty
if (!this.head) {
this.head = this.tail = new Node(value);
} else {
const oldHead = this.head; // save old head
this.head = new Node(value); // set new node to the new head
oldHead.prev = this.head; // set new head to old head prev
this.head.next = oldHead; // set old head to new head next
}
};
/**
* insert new node before a given node in the Linked List
* @param value
* @param afterValue
*/
insertNodeBefore = (value, afterValue) => {
// search current node
const currentNode = this.searchSingleNode(afterValue);
const previousNode = currentNode.prev;
// new node
// validate prev node
const newNode = new Node(value, previousNode ? previousNode : null, currentNode);
// set new node to current prev
currentNode.prev = newNode;
// validate prev node
// set new node to prev node next
if (previousNode) {
previousNode.next = newNode;
}
};
/**
* insert new node after a given node in the Linked List
* @param value
* @param afterValue
*/
insertNodeAfter = (value, afterValue) => {
// search current node
const currentNode = this.searchSingleNode(afterValue);
const nextNode = currentNode.next;
// new node
// validate next node
const newNode = new Node(value, currentNode, nextNode ? nextNode : null);
// set new node to current next
currentNode.next = newNode;
// validate next node
// set new node to next node prev
if (nextNode) {
nextNode.prev = newNode;
}
};
/**
* delete selected node(s) from the Linked List
* @param value
*/
deleteNode = (value) => {
// validate if head not exists
// happen when list is completely empty
if (!this.head) return;
// start from head
let currentNode = this.head;
// traverse Linked List
while (currentNode) {
// compare value
if (currentNode.value === value) {
const prevNode = currentNode.prev; // prev node
const nextNode = currentNode.next; // next node
// case 1: middle
// case 2: head
// case 3: tail
if (prevNode && nextNode) {
nextNode.prev = prevNode; // set next node prev
prevNode.next = nextNode; // set prev node next
} else if(!prevNode) {
nextNode.prev = null;
this.head = nextNode; // set head to the next node
} else {
prevNode.next = null;
this.tail = prevNode; // set tail to the prev node
}
}
currentNode = currentNode.next;
}
};
/**
* search a node in the Linked List
* @param value
*/
searchSingleNode = (value) => {
// validate if head not exists
// happen when list is completely empty
if (!this.head) return;
// start from head
let currentNode = this.head;
// traverse Linked List
while (currentNode) {
// compare value
if (currentNode.value === value) {
return currentNode;
}
currentNode = currentNode.next;
}
// if nothing is found
return null;
};
/**
* search all occurances of a node in the Linked List
* @param value
*/
searchAllNodes = (value) => {
// nodes array
const matchingNodes = [];
// validate if head not exists
// happen when list is completely empty
if (!this.head) return;
// start from head
let currentNode = this.head;
// traverse Linked List
while (currentNode) {
// compare value
if (currentNode.value === value) {
matchingNodes.push(currentNode);
}
currentNode = currentNode.next;
}
// return matching nodes
if (matchingNodes && matchingNodes.length > 0) {
return matchingNodes;
}
// if nothing is found
return null;
};
/**
* find the total size of the Linked List
*/
sizeOfLinkedList = () => {
// get all nodes
const allNodes = this.showLinkedList();
// return size
return allNodes && allNodes.length ? allNodes.length : 0;
};
/**
* count all occurances of a node in the Linked List
* @param value
*/
countNodeOccurances = (value) => {
// get all matching nodes
const allMatchingNodes = this.searchAllNodes(value);
// count and return all matching nodes
return allMatchingNodes && allMatchingNodes.length > 0
? allMatchingNodes.length
: 0;
};
/**
* output the Linked List
*/
showLinkedList = () => {
// nodes array
const nodes = [];
// start from head
let currentNode = this.head;
// traverse Linked List
while (currentNode) {
// push node to the nodes array
nodes.push(currentNode);
// set next node to the currentNode
currentNode = currentNode.next;
}
// return nodes
return nodes;
};
}