JSFiddle - React, Tailwind, and code Playground
assign 10
by dhizzybusy
JavaScript
String.prototype.hashCode = function() {
var hash = 0, i, chr;
if (this.length === 0) return hash;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
function Node (ContentA, next) {
this.ContentA = ContentA;
this.ContentB = ContentA.toString().hashCode();
this.next = next;
}
Node.prototype = {toString: function() {return this.ContentA;}}
function LinkedList (){
this.current = null;
this.count = 0;
this.first = null;
}
LinkedList.prototype = {
last: function (node) {
if (!node) node = this.current;
return node.next? this.last(node.next) : node;
},
insert: function (node) {
if (!this.current) this.current = this.first = node;
else this.last().next = node;
this.count++;
},
move: function (num) {
if (!num) return this.current;
this.current = this.current.next;
return this.move(num -1);
},
getList: function () {
var list = [(this.current = this.first)];
for (var i=1; i< this.count; i++, this.move(1))
list.push(this.current.next);
return list;
}
};
var list = new LinkedList();
for(var i=0; i<10; i++) {
list.insert(new Node(parseInt(Math.random()*10000)));
}
nodes = list.getList();
for(var i=0; i<nodes.length; i++) {
console.log(nodes[i].ContentA + " -> " + nodes[i].ContentB);
}
console.log('\n\nafter sorting');
nodes.sort(function(a, b) {
return a.ContentB - b.ContentB;
});
for(var i=0; i<nodes.length; i++) {
console.log(nodes[i].ContentA + " -> " + nodes[i].ContentB);
}