Assignment 4

by Kristy Bond

HTML

<html>

  <body>
    <h1>
The Double Linked List Assignment 4
</h1>
    
    <input type='button' id='create' value='Create List' onclick='CreateList()'/>
    
      
    <input type='textbox' id='add' />
    <input type='button' id='insert' value='Add Node to List' onclick='addNode()'>
    <div id='finalresult' >
    
    </div>
  </body>

</html>

JavaScript

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

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

DBLinkedList.prototype.add = function(pass) {
  var aNode = new Node();
  aNode.content = pass;

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

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

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

DBLinkedList.prototype.print = function() {
  if (this.head == null) return "Empty list, Please Add something here.";
  var result = "";
  var node = this.head;
  var increment = 0;
  while (node != null) {
    increment = increment + 1;
    result += "#" + increment + ": " + node.content + "</br>";
    node = node.next;
  }
  return result;
}

var aList = new DBLinkedList();

function CreateList(letters) {
  for (var i = 0; i < 5; i++) {
    letters = String.fromCharCode(65 + i);
    aList.add(letters);
    document.getElementById('finalresult').innerHTML = aList.print();
  }
}

var addNode = function() {
  var newValue = document.getElementById('add').value;
  aList.add(newValue);
  document.getElementById('finalresult').innerHTML = aList.print();
}