I am trying to write a linked list function that can remove the node by search value. If value matched it removes the node and link the previous node to the next node. I wrote the pseudo code, but I am having trouble implementing. The function is called removedSelected();
var LinkedList = function(){
var list = {};
//this should always point to the first node in the list
list.head = null;
list.tail = null;
list.addToTail = function(value){
var newNode = Node(value);
if(!list.head){
this.head = newNode;
this.tail = newNode;
} else {
this.tail.next = newNode;
this.tail = newNode;
}
};
list.removeSelected = function(target){
//loop through the linked list
//if head value is not equal to target
var node = this.head;
while(node.value !== target){
}
//keep going through the list
//if this.head === target
//point the previous next to the next node
};
list.indexCount = function(){
return this.indexCount;
}
list.removeHead = function(){
var deleted = this.head.value;
this.head = this.head.next;
if(this.head === null){
this.tail = null;
}
return deleted;
};
list.contains = function(target){
var node = this.head;
while(node !== null){
if(node.value === target){
return true;
}
node = node.next;
}
return false;
};
return list;
};
var Node = function(value){
var node = {};
node.value = value;
node.next = null;
return node;
};
var a = new LinkedList();
a.addToTail(1);
a.addToTail(5);
a.addToTail(55);
a.removeSelected(5);