//Block Chain implemented as a Doubly LInked List
var Node = function(_content, previousHash = '') {
this.next = null;
this.previous = null;
this.content = _content;
this.hash = this.content.toString();
this.previousHash = previousHash;
}
var List = function() {
this.head = null;
this.tail = null;
this.length = 0;
}
List.prototype.push = function(_content, previousHash) {
var node = new Node(_content, previousHash);
if (!this.head) {
this.head = node;
this.length++;
} else {
var current = this.head;
while (current.next) {
current = current.next;
}
current.next = node;
this.length++;
}
}
List.prototype.toString = function() {
var str = "";
var node = this.head;
while (node != null) {
str += node.content;
node = node.next;
}
return str;
}
//Hash Function converts to a 32 bit integer
// A more clever approach of :http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method
//was written and posted by Willsmeiser: http://mediocredeveloper.com/wp/?p=55
/*function hashCode(str) {
var hash = 0;
for (let i = 0; i < str.length; i++) {
var char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
*/
var block = new List();
block.push('One');
block.push('Two');
block.push('Three');
block.push('Four');
console.log(block);
/*
Assignment
You are going to create your own Blockchain (just like in the video) with only one little difference. He uses an Array to store his Blockchain, you are simply going to do this making the Blockchain a List.
Differences will be;
Block object will need a pointer to next (and previous if implemented doubly which I recommend)
Your Block will NOT need an index
Your Block will NOT need a...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.