Prep: Linked List Fully implemented list
Prep algorithm
by bladnman
HTML
<input type=button id="theButton" value="run test" class="runButton">
<div id="log" class="log"></div>
CSS
.runButton {
width: 125px;
margin: 20px;
}
.log {
padding:10px;
margin: 20px;
border: 1px dotted #ccc;
color:#888;
font-face: arial;
font-size:12px;
background: #fbfbfb;
}
JavaScript
runTest();
/* ************************************ */
function runTest() {
var list = new List();
for (var i=0; i < 10; i++) {
list.addNode(i);
}
logList(list.getRootNode());
}
function List() {
var my = this;
var rootNode = null;
var lastNode = null;
my.addNode = function(data) {
var node = { data:data, nextRef:lastNode};
if (rootNode == null) {
rootNode = node;
}
lastNode = node;
return node;
};
my.getRootNode = function() {
return rootNode;
}
}
function logList(node) {
if (typeof node == 'undefined') {
log("Nothing to log");
return;
}
var outHTML = "";
while (node !== null) {
if (outHTML.length > 0) {
outHTML += " -> ";
}
outHTML += "<span class=''>"+node.data+"</span>";
// console each
console.log(node.data);
node = node.nextRef;
}
// HTML
$("#log").append("<br>").append(outHTML);
}
/* ************************************
_____ ___ __ __ ___ _ _ _____ ___
|_ _| __| \/ | _ \ | /_\_ _| __|
| | | _|| |\/| | _/ |__ / _ \| | | _|
|_| |___|_| |_|_| |____/_/ \_\_| |___|
************************************ */
function log(message, value) {
var outMessage = message;
if (typeof value !== "undefined" && value !== null) {
outMessage += " [" + value + "]";
}
if ($("#log").html() != "") {
$("#log").append("<br>");
}
$("#log").append(outMessage);
if (window.console && console.log) {
console.log(outMessage);
}
}
$("#theButton").live('click', function() {
runTest();
});