Assignment 7

Tower of Hanoi

by Kristy Bond

HTML

Value of N: <input type="textbox" id="valueOfN" value="7" size="7" />
<br/>
<input type="button" value="Move Towers" onClick="callSolution();" />
<p id='reportOut'></p>

JavaScript

var valueToProcess = '';
var iteration = 0;
var Node = function(_content) {
  this.next = null;
  this.previous = null;
  this.content = _content;
}

var Queue = function() {
  this.front = null;
  this.back = null;


  this.push = function(_content) {

    if (this.front == null) {
      this.front = new Node(_content);
      this.back = this.front;
      return this;
    }


    var addedNode = new Node(_content);


    addedNode.previous = this.back;


    this.back.next = addedNode;


    this.back = addedNode;
    return this;
  }


  this.removeFront = function() {
    if (this.front == null) {

      return null;
    }
    var contentRemoved = this.front.content;

    if (this.back == this.front) {

      this.front = null;
      this.back = null;
      return contentRemoved;
    } else {
      this.front = this.front.next;
      this.front.previous = null;
      return contentRemoved;
    }
  }




  this.removeBack = function() {
    if (this.front == null) {

      return null;
    }
    var contentRemoved = this.back.content;
    if (this.back == this.front) {

      this.front = null;
      this.back = null;

      return contentRemoved;
    } else {
      this.back = this.back.previous;
      this.back.next = null;
      return contentRemoved;
    }
  }


  this.toString = function() {
    var str = "";
    var node = this.front;
    if (this.front == null) {
      str = "_empty_";
    } else {
      //str = "Queue: ";
    }
    while (node != null) {
      str += node.content + "&nbsp";
      node = node.next;
    }
    return str;
  }


  this.countElements = function() {
    var countX = 0;
    var node = this.front;
    while (node != null) {
      node = node.next;
      countX = countX + 1;
    }
    return countX;
  }
}

function recursiveSolution(x, towerFrom, towerTo, towerUtility) {
  if (x > 1) {
    recursiveSolution(x - 1, towerFrom, towerUtility, towerTo);
    recursiveSolution(1, towerFrom, towerTo, "");
    recursiveSolution(x - 1,...