forked from AllAlgorithms/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglyLinkedList.js
More file actions
233 lines (218 loc) · 5.22 KB
/
singlyLinkedList.js
File metadata and controls
233 lines (218 loc) · 5.22 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
/**
* @author Rashik Ansar
*
* Implementation of singly linked list
* Singly linked list is a linear data strucutre
*/
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class SinglyLinkedList {
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 {SinglyLinkedList} LinkedList after adding new node as tail
*/
push(data) {
let temp = new Node(data);
if (!this.head) {
this.head = temp;
this.tail = this.head; // temp
} else {
this.tail.next = temp;
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 {SinglyLinkedList} LinkedList after adding a new node as head
*/
unshift(data) {
let temp = new Node(data);
if (!this.head) {
this.head = temp;
this.tail = this.head;
} else {
temp.next = this.head;
this.head = temp;
}
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 {SinglyLinkedList} 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.next = temp;
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 current = this.head;
let temp = current;
while (current.next) {
temp = current;
current = current.next;
}
this.tail = temp;
this.tail.next = null;
this.length--;
this.emptyListCheck();
return current;
}
/**
* 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;
this.head = current.next;
this.length--;
this.emptyListCheck();
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 previous = this.get(index - 1);
let temp = previous.next;
previous.next = temp.next;
this.length--;
return temp;
}
/**
* 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 counter = 0;
let current = this.head;
while (counter !== index) {
current = current.next;
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 {SinglyLinkedList} LinkedList
*/
set(index, data) {
// Here error checking will be done by the get method itself
// No need to specify explicitly
let existedNode = this.get(index);
if (existedNode) {
existedNode.data = data;
return this;
}
}
/**
* Reversing the Linked list
* @returns {SinglyLinkedList} LinkedList
*/
reverse() {
let temp = this.head;
this.head = this.tail;
this.tail = temp;
let previous = null;
let after = null;
while (temp) {
after = temp.next;
temp.next = previous;
previous = temp;
temp = after;
}
return this;
}
/**
* Traversing or Printing the Linked list
*/
traverse() {
let current = this.head;
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;
}
/**
* Utility Function (PRIVATE FUNCTION)
* if the length is zero then assign null to both head and tail
*/
emptyListCheck() {
if (this.length === 0) {
this.head = null;
this.tail = null;
}
}
}