COP3530 Linked List

A starter project for the COP3530 Linked List

by ClarenceDowns

HTML

<br/>
<br/>
<input type="button" id="CreateList" value="Create List" onClick="createList();" />
<br/><br/> Name of Node being added:
<input type="textbox" id="LinkName" Value="" />
<input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
<p id="demo"></p><br/>
<p id="demo2"></p>
<br/>

JavaScript

//Doubly Linked List
// When button is clicked createList() is called which displays first 5 nodes in List
function createList() {
  var value = document.getElementById("LinkName").value;
  document.getElementById("demo").innerHTML = list.print();
}

// When button is clicked addNode() is called which adds new node
// created by user to the Linked List
function addNode() {
  let value = document.getElementById("LinkName").value;
  list.addNode(value);
  document.getElementById("demo").innerHTML = list.print();
}


// Define the link object
function Node(_value, _last) {
  this.value = _value; // The value stored 
  this.last = _last; // A pointer to the previous link
  this.next = null; // a pointer to the next link
  return this; // returns the created node
}

Node.prototype.asString = function() {
  return "Node Value: " + this.value + "<br/>";
};


// Define the List object
function List(_value) { // We will define the list with the first link defined
  //this.length = 0;
  this.head = null; // Pointer TO the head is null
  this.last = this.head; // When created - head and last are the same.
}

List.prototype.addNode = function(value) {
  node = new Node(value);
  let current;
  if (this.head == null) {
    this.head = node;
  } else {
    current = this.head;
    while (current.next != null) {
      current = current.next;
    }
    current.next = node;
    node.previous = current;
    node.next = null;

  }
}
// This is a very simple print routine - it starts at the head
// and moves through the list
List.prototype.print = function() {
  var s = "";
  var n = this.head;

  while (n != null) {
    s += n.asString();
    n = n.next;
  }

  return s;
}

let list = new List();
list.addNode("Football");
list.addNode("Basketball");
list.addNode("Soccor")
list.addNode("Track");
list.addNode("Swimming");
list.print();

/* Assignment 4

In Javascript we are going to create a Doubly Linked List.If you need a little primer on creating objects and classes in Javascript...