-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList js
49 lines (41 loc) · 879 Bytes
/
linkedList 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
// linkedlist class
class LinkedList {
constructor()
{
this.head = null;
this.size = 0;
}
// functions to be implemented
// add(element)
// insertAt(element, location)
// removeFrom(location)
// removeElement(element)
// Helper Methods
// isEmpty
// size_Of_List
// PrintList
}
// adds an element at the end
// of list
add(element)
{
// creates a new node
var node = new Node(element);
// to store current node
var current;
// if list is Empty add the
// element and make it head
if (this.head == null)
this.head = node;
else {
current = this.head;
// iterate to the end of the
// list
while (current.next) {
current = current.next;
}
// add node
current.next = node;
}
this.size++;
}