MOD 3 - LIST

chain of 5 links

by SHELDON PASCIAK

HTML

<br/>
-Creates a chain of 5 links. <br />
-Prints links to screen. <br />
<p id="demo"></p>

<!--

Assignment 1 - In Javascript we are going to create a Class called Chain. 

Please note that this is a very inefficient representation of the Chain. Here is a simple Javascript code definition of a Link for you to use. https://jsfiddle.net/reaglin/q46obht6/
 
You will make a Chain consisting of 5 links. You will also create a print function for the chain that will print the links in link order. You may need to create some functions to support this functionality.

-->

CSS

* {
    
    font: 12pt verdana;
}

JavaScript

/* 
	Module 3 - Assignment 1 - SHELDON PASCIAK
    
    Consider keeping a tail object to keep track of last item
	Consider keeping prev and next values in each node
*/

var output = document.getElementById("demo"); //access document element

// for use to display output to console and in window of fiddle
function printToScreen(msg) { 
    output.innerHTML += msg + "<br />";
    console.log(msg);
}

// Define the link object
function Link(_id, _value, _next) {
    this.id = _id; // The id of the current link
    this.value = _value; // The value stored 
    this.next = _next; // a pointer to the next link, this is 0 if it is the last link in the chain
}

Link.prototype.asString = function () {
    return "Link: " + this.id +
        " Value: " + this.value +
        " Points To: " + this.next ;
};

// Define the Chain object
function Chain(_firstValue) { // We will define the chain with the first link defined
    this.length = 1; // I like to keep track of the first link, not really necessary
    this.head = new Link(1, _firstValue, 0);    
    this.linkStorage = []; // Array for back storage
    this.linkStorage.push(this.head); // A chain has at least a head
}

/*
	Merely swaps underlying back storage items at indexes to demonstrate this should have no affect on printed order. TODO: Consider how to swap or rearrange the path .. i.e. if each node was a name of a city, the path might be CHICAGO-DETROIT-ATLANTA-DAYTONA, consider the ability to swap (DETROIT-ATLANTA) so the new Head...end order would be CHICAGO-ATLANTA-DETROIA-DAYTONA.  
*/
Chain.prototype.swap = function (i1,i2) { 
    console.log("Swapping indexes in internal array: " + i1 + "," + i2);
    var temp ;
    temp = this.linkStorage[i1];
    this.linkStorage[i1] = this.linkStorage[i2];
    this.linkStorage[i2] = temp;        
}

/*
	Iterates all elements of the back storage array (linkStorage) looking for node with the ID to search for.  Returns with null object when ID not found, or the node object...