Assignment_4_jones-baker

by davidjb95

HTML

<input type="textbox" id="LinkName" Value="New Node" />
<input type="button" id="CreateList" value="Create List" onClick="createList();" />
<input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
<p id="output">
</p>

JavaScript

var list = ;
function createList() {
	var value = document.getElementById("LinkName").value;
  list = new List(value);
  document.getElementById("output").innerHTML = list.print();
}

function addNode() {
  var value = document.getElementById("LinkName").value;
  list.addNode(value);
  document.getElementById("output").innerHTML = list.print();
}
function Node(_value, _last) {
	this.value = _value;
  this.last = _last;
  this.next = null;
  return this;
  
}
Node.prototype.asString = function() {
	return "Node Value:" + this.value + "<br/>";
}
function List(_value) {
	this.length = 1;
  this.head = new Node(_value, null);
  this.last = this.head;
}

List.prototype.addNode = function(_value) {
if(tail == null){
  	head = _value;
    tail = _value;
    length = 1;
  }
  if(head == tail){
  	head.next = _value;
    tail = _value;
    length++;
  }
  if(tail != null){
  	tail.next = _value;
    length++;
  }
}

List.prototype.print = function() {
	var s = "";
  var n = this.head;
  while (n != null){
  	s += n.asString();
    n = n.next;
  }
		return s;
}