I've been studying data structures and have taken a stab at implementing a singly LL in JavaScript. Please review and advise on any additional improvements and optimizations:
function Node(val){
this.data = val;
this.next = null;
};
function SinglyList(){
this._length = 0;
this.head = null;
};
//Add
SinglyList.prototype.add = function(val){
var node = new Node(val),
currentNode = this.head;
//if list is empty
if(!currentNode){
this.head = node;
this._length++;
return;
}
//locate tail
while(currentNode.next){
currentNode = currentNode.next;
}
//Add to the tail
currentNode.next = node;
this._length++;
return node;
}
//isEmpty
SinglyList.prototype.isEmpty = function(){
return this._length === 0;
}
//Contains
SinglyList.prototype.contains = function(val){
var currentNode = this.head;
if(this.isEmpty()) return;
while(currentNode){
if(currentNode.data === val) return true;
currentNode = currentNode.next;
}
return false;
}
//Reversed
SinglyList.prototype.reverse = function(){
var currentNode = this.head,
prev = null;
while(currentNode){
var temp = currentNode.next;
currentNode.next = prev;
prev = currentNode;
currentNode = temp;
}
this.head = prev;
return this;
}
//RemoveAt
SinglyList.prototype.removeAt = function(index){
if(index<0 || index > this._length)
return;
var currentNode = this.head, i=0, prev;
//if 1st in the list
if(index === 0){
this.head = currentNode.next;
this._length--;
return this;
}
//Subsequent items in the list
while(i++ < index-1){
prev = currentNode;
currentNode = currentNode.next;
}
prev.next = currentNode.next;
this.head = prev;
this._length--;
return this;
}
//Remove by value
SinglyList.prototype.removeByVal = function(val){
if(this.isEmpty()) return;
if(!this.contains(val)) return;
var currentNode = this.head;
if(currentNode.data===val){
this.head = currentNode.next;
this._length--;
return this;
}
while(currentNode.next){
if(currentNode.next.data===val){
prev = currentNode;
}
currentNode = currentNode.next;
}
prev.next = prev.next.next;
this._length--;
return this;
}
//Append
SinglyList.prototype.append = function(val){
this.add(val);
return this;
}
//Prepend
SinglyList.prototype.prepend = function(val){
var node = new Node(val),
temp = this.head;
node.next = temp;
this.head = node;
return this;
}
var singlyList = new SinglyList();
singlyList.add(5);
singlyList.add(10);
singlyList.add(15);
singlyList.add(20);
singlyList.add(25);
singlyList.add(30);
singlyList.add(35);
singlyList.add(40);
console.log('Singly LL:\n'+JSON.stringify(singlyList));
console.log('isEmpty method:\n'+singlyList.isEmpty());
console.log('Contains:\n'+singlyList.contains(5));
console.log('Reversed:\n'+JSON.stringify(singlyList.reverse()));
console.log('Remove:\n'+JSON.stringify(singlyList.removeAt(2)));
console.log('Reversed:\n'+JSON.stringify(singlyList.reverse()));
console.log('Append:\n'+JSON.stringify(singlyList.append(45)));
console.log('Prepend:\n'+JSON.stringify(singlyList.prepend(50)));
console.log('Remove by value:\n'+JSON.stringify(singlyList.removeByVal(50)));