Skip to content

Instantly share code, notes, and snippets.

@irvinemd55
Created April 5, 2017 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irvinemd55/6bab1ef85b5ad66efd590cf8f71bde4b to your computer and use it in GitHub Desktop.
Save irvinemd55/6bab1ef85b5ad66efd590cf8f71bde4b to your computer and use it in GitHub Desktop.
implimentation of a linklist
LinkedList.Circular = function() {};
LinkedList.Circular.prototype = new LinkedList();
LinkedList.Circular.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.Circular.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.Circular.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.Node = function(data) {
this.prev = null; this.next = null;
this.data = data;
};
// myList.append(new LinkedList.Node(someObject));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment