Last active
November 15, 2017 21:31
-
-
Save upex/83b27d90fe851ffb04b47cc76575028b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Node (data){ | |
this.data = data | |
this.next = null | |
} | |
Node.prototype = { | |
insertAtEnd: function(node){ | |
if(this.next === null){ | |
this.next = node | |
}else{ | |
this.next.insertAtEnd(node) | |
} | |
}, | |
inorderVisit: function(){ | |
console.log('Inorder data visit=>', this.data) | |
if(this.next != null){ | |
this.next.inorderVisit() | |
} | |
}, | |
reverseVisit: function(){ | |
if(this.next != null){ | |
this.next.reverseVisit() | |
} | |
console.log('Inorder data visit=>', this.data) | |
}, | |
search: function(val){ | |
if(this.data === val){ | |
return this | |
}else if(this.next != null){ | |
return this.next.search(val) | |
} | |
return null | |
} | |
} | |
// Link list | |
function Linklist(){ | |
this.head = null | |
} | |
Linklist.prototype = { | |
addValueAtEnd: function(val){ | |
var node = new Node(val) | |
if(this.head === null){ | |
this.head = node | |
}else{ | |
this.head.insertAtEnd(node) | |
} | |
}, | |
addValueAtBegin: function(val){ | |
var node = new Node(val) | |
if(this.head === null){ | |
this.head = node | |
}else{ | |
var tmp = this.head | |
node.next = tmp | |
this.head = node | |
} | |
}, | |
addValueAtMidle: function(addthis, afterthis){ | |
var newnode = new Node(addthis) | |
if(this.head !== null){ | |
var previousnodeFound = this.head.search(afterthis) | |
if(previousnodeFound){ | |
var previousNextTmp = previousnodeFound.next | |
previousnodeFound.next = newnode | |
newnode.next = previousNextTmp | |
}else{ | |
console.log('No elemnt exits') | |
} | |
}else{ | |
console.log('Empty list') | |
} | |
}, | |
inorderVisit: function(){ | |
if(this.head != null){ | |
this.head.inorderVisit() | |
}else{ | |
console.log('No element found') | |
} | |
}, | |
reverseVisit: function(){ | |
if(this.head != null){ | |
this.head.reverseVisit() | |
}else{ | |
console.log('No element found') | |
} | |
}, | |
searchList: function(val){ | |
if(this.head != null){ | |
var found = this.head.search(val) | |
if(found){ | |
return found | |
}else{ | |
console.log('Not found') | |
} | |
}else{ | |
console.log('Empty list') | |
} | |
}, | |
deleteFirstNode: function(){ | |
if(this.head != null){ | |
var firstNodeNext = this.head.next | |
this.head = firstNodeNext | |
} | |
}, | |
deleteLastNode: function(){ | |
if(this.head != null){ | |
var currentNode = this.head | |
var prviousNode | |
while(currentNode.next != null){ | |
prviousNode = currentNode; | |
currentNode = currentNode.next | |
} | |
prviousNode.next = null | |
} | |
}, | |
deleteAnyNode: function(val){ | |
if(this.head != null){ | |
var found = this.head.search(val) | |
if(found.next==null){ | |
this.deleteLastNode() | |
}else{ | |
console.log('found', found) | |
var currentNode = this.head | |
var prviousNode | |
while(currentNode.next != null){ | |
prviousNode = currentNode; | |
currentNode = currentNode.next | |
if(currentNode.data = found.data){ | |
break; | |
} | |
} | |
prviousNode.next = found.next | |
} | |
} | |
} | |
} | |
var list = new Linklist() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment