Chain and Link Starter
A starter project for the COP3530 Chain and link
by wooozy
HTML
This is a demonstration of the concept of making a Linked List (in this case we call it a chain in JavaScript). There are 2 object involved here - and this is a very easy conceptual way to do this.
<br/>
<br/> Add Link Name:
<input type="textbox" id="LinkName" Value="New Link" />
<input type="button" id="AddLink" value="Add Link" onClick="addLink();" />
<input type="button" id="DisplayHead" value="Display Chain Head" onClick="displayChainHead();" />
<p id="demo"></p>
JavaScript
// Let's try it by making a single link chain and testing it.
var chain = new Chain('Link 1');
var chain2 = new Chain("Link 2");
var chain3 = new Chain("Link 3 ");
var chain4 = new Chain("Link 4");
var chain5 = new Chain("Link 5");
function addLink() {
var newLinkName = document.getElementById("AddLink").value;
alert("Adding a Link Named: " + firstLinkName);
chain.addLink(newLinkName);
chain.print();
console.log(chain);
secondLink();
}
function secondLink(){
var secondLinkName = document.getElementbyID("SecondLink").value;
alert("Adding a Link Named:" +secondLinkName);
chain2.secondLink(secondLinkName);
chain2.print();
thirdLink();
}
function thirdLink(){
var thirdLinkName = document.getElementbyId("ThirdLink").value;
alert("Adding a new Link Named:" +thirdLinkName);
chain.thirdLink(thirdLinkName);
chain.print();
fourthLink();
}
function fourthLink(){
var fourthLink = document.getElementbyId("FourthLink").value;
alert("Adding a new Link Named:" +fourthLinkName);
chain.fourthLink(fourthLinkName);
fifthLink();
}
function fifthLink(){
var fitfthLink = document.getElementbyId("FifthLink").value;
alert("Adding a new Link Named:" + fifthLinkName);
chain.fifthLink(fifthLinkName)
}
function displayChainHead() {
// This should print out - just a sample output
document.getElementById("demo").innerHTML = chain.head.asString();
}
// Define the link object
function Link(_id, _value, _next) {
// This is a possible implementation of a Link - this requires 3 pieces of information and links have an ID. Note - also not required for successful implementation.
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 + "<br/>";
}
Link.prototype.setPointer = function(_next) {
// A...