// Here I have created a DoubleLinkedList, which points each node back at each other. The function makes sure that when the list is manipulated that the nodes will still be pointing in the right direction.
var DoubleLinkedList = function() {
this.head = null;
this.tail = null;
this.length = 0;
this.clear = function() {
this.head = null;
this.tail = null;
this.length = 0;
};
this.add = function(content) {
if (this.head == null) {
this.head = new LinkedListNode(content);
this.length++;
return this.head;
}
if (this.tail == null) {
this.tail = new LinkedListNode(content);
this.head.next = this.tail;
this.tail.last = this.head;
this.length++;
return this.tail;
};
this.tail.next = new LinkedListNode(content);
this.tail.next.last = this.tail;
this.tail = this.tail.next;
this.tail.next = null;
this.length++;
return this.tail;
};
this.copy = function() {
var newlist = new DoubleLinkedList();
var node = this.head;
while (node != null) {
newlist.add(node.content);
node = node.next;
}
return newlist;
}
this.pop = function() {
if (this.head == null) {
this.length = 0;
return null;
}
// Case of one node
if (this.length == 1) {
var a = this.head;
this.tail = null;
this.head = null;
this.length = 0;
return a;
}
// Case of length > 1
var a = this.head; // hold value for return
this.head = this.head.next;
this.head.last = null;
this.length--;
return a;
};
//This is my string function to state how items are in the list.
this.toString = function() {
var str = "The list has a length of " + this.length + " items" + "<br/>";
var node = this.head;
while (node != null) {
str += node.content + "<br/>";
node = node.next;
}
return str;
};
}
var LinkedListNode = function(content) {
this.next = null;
...
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.