COP3530 Linked List

A starter project for the COP3530 Linked List

by Ron Eaglin

HTML

This is a demonstration of the concept of making a Linked List. There are 2 object involved here List and Node.
<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="New Node" />
<input type="button" id="AddLink" value="Add Node to List" onClick="addNode();" />
<p id="demo"></p>
<br/>
You should be able to build both Doubly and Singly Linked Lists - read more at <a href ='https://en.wikipedia.org/wiki/Linked_list'>Wikipedia Linked Lists</a>

JavaScript

var list = null;

function createList() {
// THis function will instantiate the global variable list as an
// actual list.  The constructor for the list requires a single 
// argument. T

// If you look at hte constructor for the List you will see that 
// you aldso create a Node when you create the list.
  var value = document.getElementById("LinkName").value;
  list = new List(value);
  document.getElementById("demo").innerHTML = list.print();
}


function addNode() {
  var value = document.getElementById("LinkName").value;
  list.addNode(value);
  document.getElementById("demo").innerHTML = list.print();
}


// Define the link object
function Node(_value, _last) {
  // This is a possible implementation of a Doubly Linked List
  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
// This is the constructor for a List. It creates a list object. It also 
// creates teh first node in the list. This Node is the head. This is a 
// proerty of the list. The list we are creating has 3 properties; length,
// head, and last. 
// 
// This is just one way to define a list. You will learn others.
function List(_value) { // We will define the list with the first link defined
  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.
}

// Here is where you will beed to start filling in the blanks. When you 
// add anode to a list you need to point the next property of the previous 
// node to the one you created. My video should explain this well. 
List.prototype.addNode = function(_value) {
  // A function to add a link to the list - you will have to write this
  alert("This function must be written");
  
}

// This is a...