Assignement 4

by Jeremy Boss

HTML

Create A New List, Then Add A Node!
<br/>
<br/>
<input type="textbox" id="values" />
<input type="button" value="Create New List" onClick="createList()" />
<input type="button" value="Add a Node" onClick="addNode()" />

<div id="out">

</div>

JavaScript

var newList = new DoublyLinked();



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

function NewNode() {
  this.values = null;
  this.next = null;
  this.prev = null;
  return this;
}

DoublyLinked.prototype.add = function(values) {
  var node = new NewNode();
  node.values = values;

  if (this.head === null) {
    this.head = node;
    this.length = 1;
    return node;
  }

  if (this.tail === null) {
    this.tail = node;
    this.tail.prev = 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;
}


function addNode() {
  var values = document.getElementById("values").value;
  newList.add(values);
  newList.print();
}

DoublyLinked.prototype.print = function() {
  if (this.head === null) return "Empty";
  var display = " ";
  var counter = 0;
  var node = this.head;
  while (node !== null) {
    counter = counter + 1;
    display += "Node: " + counter + " Value: " + node.values + "</br>";
    node = node.next;
  }
  document.getElementById("out").innerHTML = display;
}

function createList(values) {

  newList.add("A")
  newList.add("B")
  newList.add("C")
  newList.add("D")
  newList.add("E")
  newList.print();
  
}