Skip to content

Instantly share code, notes, and snippets.

@kaliaparijat
Created January 2, 2013 19:30
Show Gist options
  • Save kaliaparijat/4437177 to your computer and use it in GitHub Desktop.
Save kaliaparijat/4437177 to your computer and use it in GitHub Desktop.
Javascript implementation of a customized LinkedList. I use this as the slides for a custom jQuery carousel that is limited to only 2 list elements.
function LinkedList(){};
LinkedList.prototype = {
length:0,
first:null,
last:null,
cursor:null
}
LinkedList.DoubleCircular = function(){};
LinkedList.prototype.isEmpty = function(){
if(this.length == 0)
return true;
return false;
};
LinkedList.DoubleCircular.prototype = new LinkedList();
LinkedList.DoubleCircular.prototype.append = function(node){
if(this.first === null){
node.prev = node;
node.next = node;
this.first = node;
this.last = node;
}else{
node.prev = this.last;
node.next = this.first;
this.first.prev = node;
this.last.next = node;
this.last = node;
}
this.length++;
};
LinkedList.DoubleCircular.prototype.insertAfter = function(node,newNode){
newNode.prev = node;
newNode.next = node.next;
node.next.prev = newNode;
node.next = newNode;
if(newNode.prev == this.last) { this.last = newNode; }
this.length++;
};
LinkedList.DoubleCircular.prototype.remove = function(node){
if(this.length > 1 ) {
node.prev.next = node.next;
node.next.prev = node.prev;
if(node == this.first) this.first = node.next;
if(node == this.last) this.last = node.prev;
}
else{
this.first = null;
this.last = null;
}
node.prev = null;
node.next = null;
this.length--;
};
LinkedList.DoubleCircular.prototype.reset = function(){
while(this.length > 0)
{
this.remove(this.last);
}
};
LinkedList.prototype.setCursor = function(node){
this.cursor = node;
};
LinkedList.prototype.getCursor = function(){
return this.cursor;
};
LinkedList.Node = function(data){
this.prev = null;
this.next = null;
this.data = data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment