//Linked List object
function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
}
//Node object
function Node(){
this.next = null;
this.prev = null;
this.content = null;
}
//function to add content to Node
LinkedList.prototype.add = function(_content){
var node = new Node();
node.content = _content;
if (this.head == null){
this.head = node;
this.length = 1;
return node;
}
if (this.tail == null){
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length++;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
//print the list in a way that is readable
LinkedList.prototype.print = function(){
if (this.head == null) return "Empty List";
var string = "";
var node = this.head;
while (node != null) {
string += node.content + " ";
node = node.next;
}
return string;
}
//instantiate a new list
var aList = new LinkedList();
//function to create a list containing 5 nodes
function createList(){
aList.add("A");
aList.add("B");
aList.add("C");
aList.add("D");
aList.add("E");
document.getElementById("output").innerHTML = aList.print();
}
//function to add a new node to the end of the list
function addNode(){
var c = document.getElementById("tbNode").value;
aList.add(c);
document.getElementById("output").innerHTML = aList.print();
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.