The complexity is O(2^n)
<br/>
We should not be concerned because (2^64)*2 is equal to around 1 trillion years, and I will be dead long before then. Whoever is alive then can figure out that problem on their own.
<br/>
<input type="textbox" id="item_tb" />
<input type="button" id="push_button" value="Start Hanoi" onClick="startHanoi();" />
<br/>
<div id="stack_div"></div>
JavaScript
// Node
var Node = function (value, last)
{
this.next = null;
this.last = last;
this.value = value;
}
// Stack
var Stack = function()
{
this.head = null;
this.top = null;
this.size = 0;
this.push = function(value)
{
if(this.head == null) {
this.head = new Node(value, null);
this.top = this.head;
}
else
{
var newNode = new Node(value, this.top);
this.top.next = newNode;
this.top = newNode;
}
this.size++;
return this;
}
this.pop = function()
{
var oldTop = this.top;
if(this.top != null) {
if(this.top.last == null) {
this.top = null;
this.head = null;
}
else
{
this.top = this.top.last;
this.top.next = null;
}
this.size--;
}
return oldTop;
}
this.clear = function()
{
this.head = null;
this.top = null;
this.size = 0;
}
this.print = function()
{
var string = "";
var node = this.head;
while(node != null) {
string += node.value + " ";
node = node.next;
}
return string;
}
}
// Other Functions
document.getElementById("item_tb").addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode == 13) {
document.getElementById("push_button").click();
}
});
var oCount = 0;
var rodl = new Stack(); // Rod left
var rodm = new Stack(); // Rod middle
var rodr = new Stack(); // Rod right
function startHanoi()
{
var item = document.getElementById('item_tb');
if(item.value < 11)
{
rodl.clear();
rodm.clear();
rodr.clear();
document.getElementById('stack_div').innerHTML = "";
oCount = 0;
for(i = item.value; i > 0; i--)
{
rodl.push(i);
}
printRods();
moveDisk(item.value, rodl, rodr, rodm);
document.getElementById('stack_div').innerHTML = "Stack=" + item.value + "<br/>Move Count=" + oCount + "<br/><br/>" + document.getElementById('stack_div').innerHTML;
}
else
{
alert("Anything greater than 10 will not be accepted because it takes to long to calculate.")
}
item.value =...
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.