A simple implementation of
LinkedList
along with traversal can be done like this in JavaScript:(function(){
'use strict';
var LinkedList = function(){
this.head = null;
}
LinkedList.prototype.appendToTail = function(data){
var node = {
"data":data,
"next":null
};
if(this.head == null){
this.head = node;
}else{
var current = this.head;
while( current.next != null){
current = current.next;
}
current.next = node;
}
}
LinkedList.prototype.traverseList = function(){
var current = this.head;
while(current != null){
console.log(current.data);
current = current.next;
}
}
var linkedList = new LinkedList();
linkedList.appendToTail(20);
linkedList.appendToTail(30);
linkedList.appendToTail(40);
linkedList.traverseList();
})()
No comments:
Post a Comment