enter the number of disks: <input id='uInput' type='number'><br/>
<button id='calc'>
Calculate number of moves
</button>
<div id='output1'>
</div>
JavaScript
document.getElementById("calc").addEventListener("click", function(){
run();
});
var Node = function(_content) {
this.next = null;
this.last = null;
this.content = _content;
}
var Stack = function(_desc) {
this.desc = _desc;
this.bottom = null;
this.top = null;
this.push = function(_content) {
// No head - create one
if (this.bottom == null) {
this.bottom = new Node(_content);
this.top = this.bottom;
return this;
}
var addedNode = new Node(_content);
addedNode.last = this.top; // pointer to previous node
this.top.next = addedNode; // current top points to new
this.top = addedNode; // which becomes new top
return this;
}
this.pop = function() {
if (this.bottom == null) {
return null;
}
// Case of one node
if (this.bottom == this.top) {
this.bottom = this.top;
return this.top.content;
}
// Now remove top Node
var a = this.top; // hold value for return
this.top = this.top.last;
this.top.next = null;
return a.content;
}
}
Stack.prototype.toString = function() {
var str = this.desc + ": ";
var node = this.bottom;
while (node != null) {
str += node.content + " ";
node = node.next;
}
return str;
}
var num = 0;
var run = function() {
var stackA = new Stack("A");
var stackB = new Stack("B");
var stackC = new Stack("C");
//var num = 0;
//Populate stackA
var i = parseInt(document.getElementById('uInput').value);
for (var t = i; t > 0; t--) {
stackA.push(t);
}
document.getElementById('output1').innerHTML = '<br/>Stack A Before: ' + stackA.toString();
document.getElementById('output1').innerHTML += '<br/>Stack B Before: ' + stackB.toString();
runHanoi(i, stackA, stackB, stackC);
document.getElementById('output1').innerHTML += '<br/>Stack A After: ' +...
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.