Created
April 5, 2017 17:48
-
-
Save irvinemd55/6bab1ef85b5ad66efd590cf8f71bde4b to your computer and use it in GitHub Desktop.
implimentation of a linklist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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