I actually implemented one example for linked lists in javascript, which is more accessible for display:
function Link(k, d) {
this.obj = {
'key': k,
'data' : d,
'next' : null
};
return this.obj;
}
function List() {
this.firstLink = new Link();
this.insertFirst = function(key, data) {
this.newLink = new Link(key, data);
this.newLink.next = this.firstLink;
this.firstLink = this.newLink;
}
this.getFirst = function() {
return this.firstLink;
}
this.removeFirst=function() {
var temp = this.firstLink;
this.firstLink = this.firstLink.next;
delete temp;
}
this.displayList=function() {
this.current = this.firstLink;
while ( this.current != null ) {
console.log(this.current);
this.current = this.current.next;
}
}
}
var lst = new List();
lst.insertFirst(22, 'ilian');
lst.insertFirst(55, 'xoxo');
lst.insertFirst(77, 'fefe');
lst.displayList();
source
share