Assignment 4

by Taylor Zimmerman

HTML

<html>
  <Body>
  <fieldset>
  <legend>Create List of Nodes</legend>
  <input type="button" value="Create List" onclick="CreateList()">
  </fieldset>
  <fieldset>
  <legend>Add Node to the List</legend>     <input type="textbox" id="tbInput"><br>
  <input type="button" value="Add Node" onclick="AddNode()">
  </fieldset>
  <fieldset>
  <legend>List</legend>
  <div id="output">
   </div>
  </fieldset>
  </Body>
</html>

JavaScript

var List = new LinkedList();


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

function LinkedList(){
	this.head = null;
  this.tail = null;
  this.length = 0;
 
}

LinkedList.prototype.add = function(pass){
	var node = new Node();
  node.content = content;
  
  if (this.head === null) {
    this.head = node;
    this.length = 1;
    return node;
  }

  if (this.tail == null) {
    this.tail = node;
    this.tail.last = this.head;
    this.head.next = this.tail;
    this.length = 2;
    return node;
  }

  this.tail.next = node;
  node.prev = this.tail;
  this.tail = node;
  this.length++;
  return node;
}

LinkedList.prototype.print = function(){
	if (this.head == null) return "Empty List";
	var node = this.head; 
  var d = "";
  	while(node != null){
  	d +="<div>Node Value: " + node.content + "</div> ";
  node = node.next;
  }
  return d;
}

function CreateList(content){
  for (var i = 0; i < 5; i++){
    content = String.fromCharCode(65 + i);
    List.add(content);
    document.getElementById('output').innerHTML = List.print();
	}
}
function AddNode(){
	var addNode = document.getElementById("tbInput").value;
  List.add(addNode);
  document.getElementById('output').innerHTML = List.print();
}