Assignment 4

Double Linked List

by Peyton Hessler

HTML

<input type="button" id="newDoubleLinkedList" value="Create New DLL" onClick="newDoubleLinkedList();" />
<br/><br/> Add new node:
<input type="textbox" id="nodeName" Value="" />
<input type="button" id="addNode" value="Add Node" onClick="addNode();" />

<p id="output"></p>

JavaScript

//create the list
function newDoubleLinkedList() {
  var value = document.getElementById("nodeName").value;
  list = new List(value);
  //defining the list
  var list = new doubleLinkedList();
  var id =;
  id=0
  list = new doubleLinkedList(null, 0, null)
  list.add('1','A');
  list.add('2', 'B');
  list.add('3', 'C');
  list.add('4', 'D');
  list.add('5', 'E');
  document.getElementById("output").innerHTML = doubleLinkedList.print();
}
//add in a node
function addNode() {
  var value = document.getElementById("nodeName").value;
  list.addNode(value);
  document.getElementById("output").innerHTML = list.print();
}
//the link object
function node(id,content,next,last) {
	this.id=id;
  this.content=content;
  this.next=next;
  this.last=last;
  return this;
}

Node.prototype.asString = function() {
  return "The new node is:" + this.content + "<br/>";
}
//Define list object
function List(value) {
  this.length = 1;
  this.head = new Node(value, null); // Pointer TO the head is null
  this.last = this.head;  // When created - head and last are the same.
}

DoubleLinkedList.prototype.length = function() {
  var i = 0;
  var node = this.head;

  while (node != 0) {
    i++;
    node = node.next;
  }
  return i;
};

DoubleLinkedList.prototype.toString = function() {
  
  var str = 'Linked List with ' + this.length + ' nodes <br/>';
  var node = this.head;


  while (node != 0) {
    str +="Node ID:" + node.id +  ': Node Value: ' + node.content;
   
    
    str += "<br>";
    node = node.next;
  }
  return str;
};