A7 Tower

Tower of Hanoi

by scotp71

HTML

Please enter the number of disks: <input id="uInput" type="textbox" value="5"><br>


<input type="button" id="calc" value="Click to Calculate" onClick="run()"/>

<div id="output1">
</div>

JavaScript

function Node(content){
	this.content=content;
  this.next=null;
  this.last=null;
}

function Stack(){
	this.length = 0;
  this.head = null;
  this.top = null;
}

Stack.prototype.push = function(content){
	//if there is no head set new node to head of list
  if(this.head==null){
  	this.head=new Node(content)
    //set top equal to head because they are the same thing
    this.top = this.head;
    this.length +=1;
    return this;
  }
  //if there is a head already, create a new node
  var addedNode = new Node(content);
  addedNode.next = this.top;  //set next pointer set to exsisting top
  this.top.last = addedNode;	//exsisting top's last pointer set to new node
  this.top = addedNode;		//set the top equal to the new node
  this.length ++
  return this;
  }
  
 Stack.prototype.pop = function(){
	if(this.head == null){
  	alert("The stack is empty");
    return null
  }
  if(this.length==1){
  	var cur = this.head;
    this.head = null;
    this.top = null;
    this.length = 0;
    return cur;
  }
  //now remove the top node
	var t = this.top;  //hold value for return
  this.top = this.top.next   //current top pointer becomes the last node
  this.top.last = null;		//next pointer of last node points to null now
  this.length -=1;
  return t;
  
}

Stack.prototype.toPrint = function(){
	if (this.head == null) return ""
  var s = "";
  var node = this.head;
  while (node != null) {
  	s += node.content + ", ";
    node = node.last;
  }
  //document.getElementById("output1").innerHTML = s;
	return s;
}


//var stack = new Stack();




function run(){
	
	var stackA = new Stack();
  var stackB = new Stack();
  var stackC = new Stack();
  
  
  //fill orginal stack
  var i = parseInt(document.getElementById("uInput").value);
  for(var t = i; t > 0; t--) {
  	stackA.push(t);
  }
  debugger;
  document.getElementById("output1").innerHTML += "<br/>Stack A Before: " + stackA.toPrint();
  document.getElementById("output1").innerHTML += "<br/>Stack B Before: " +...