<h2>Tower of Hanoi</h2>
<div id='input'>
Enter number of disks to evaluate.<br />
<input id='numDisk' type='number' min='1' max='8' />
<input type='button' value='Enter' onclick='buildTow(document.getElementById("numDisk").value)' />
<input type='button' value='Reset' onclick='reset()' /><br />
<div id='error'></div>
Play<br />
<input type='button' value='Slow' onclick='towHanoi(s1.length, "s1", "s2", "s3", 1000)' />
<input type='button' value='Medium' onclick='towHanoi(s1.length, "s1", "s2", "s3", 375)' />
<input type='button' value='Fast' onclick='towHanoi(s1.length, "s1", "s2", "s3", 0)' />
</div>
<div id='output'>
<table>
<tr>
<th>S1</th><th>S2</th><th>S3</th>
</tr>
<tr>
<td id='s1'></td><td id='s2'></td><td id='s3'></td>
</tr>
</table>
<div id='counter'></div>
<div id='questions'>
Time complexity for Tower of Hanoi is O<sub>(2<sup>n</sup> - 1)</sub><br />
<h4>Question</h4>
Should we be concerned with the legend of the world ending when the 64-disk solution is physically solved if it takes 1 second for each move?<br />
<h4>Answer</h4>
(2<sup>64</sup> - 1) s = 584.5 billion years<br />
Scientist predict, the sun will burn out and turn into a white dwarf in 5 billon years.<br />
It is not possible to solve on Earth.
</div>
<h4>Moves</h4>
<div id='moves'></div>
</div>
window.onload = function() {
s1 = new List('s1', null); //create doubly linked list
s2 = new List('s2', null); //create doubly linked list
s3 = new List('s3', null); //create doubly linked list
//listen for enter key
document.getElementById('numDisk').addEventListener('keypress', function(e) {
if(e.keyCode === 13) {
buildTow(document.getElementById('numDisk').value);
}
});
}
var time = 0;
var timeouts = [];
var count = 0;
//define list
function List(_id, _content) {
this.id = _id;
this.length = 1;
this.head = new Node(_content, null); //for new list, last = null
this.tail = this.head; //for new list, head = tail
}
//define node
function Node(_content, _last) {
this.id = 1001; //node id
this.content = _content; //node contents
this.next = null; //pointer to next node
this.last = _last; //pointer to last node
}
//push - adds a node to the top of the stack
List.prototype.push = function(_content) {
if(this.head == this.tail && this.head.content == null) { //adding content for the first time?
this.head.content = _content;
}
else {
this.head = new Node(_content, this.head); //List.head points to new-node, new-node.last points to old-head
this.head.last.next = this.head; //old-head.next points to new-head
this.head.id = this.head.last.id + 1; //assign next available node id
this.length++;
}
this.print();
}
//pop - removes the last node from the stack
List.prototype.pop = function() {
var temp = this.head.content;
if(this.head == this.tail) { //if it's the last node in the stack
this.head.content = null;
} else {
this.head = this.head.last;
this.head.next = null;
this.length--;
}
this.print();
return temp;
}
//print tower
List.prototype.print = function() {
if(this.head.content == null) { //if the tower is empty
document.getElementById(this.id).innerHTML = '';
return;
}
var current = this.head;
var printList = '';
while(current != null) { //prepare tower for printing...
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.