Created
January 11, 2019 05:06
-
-
Save 197291/23058e0aa9309282d75c032aa734ac47 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function Node(value) { | |
this.data = value; | |
this.previous = null; | |
this.next = null; | |
} | |
function DoublyList() { | |
this._length = 0; | |
this.head = null; | |
this.tail = null; | |
} | |
DoublyList.prototype.add = function(value) { | |
const node = new Node(value); | |
if (this._length) { | |
this.tail.next = node; | |
node.previous = this.tail; | |
this.tail = node; | |
} else { | |
this.head = node; | |
this.tail = node; | |
} | |
this._length++; | |
return node; | |
}; | |
DoublyList.prototype.searchNodeAt = function(position) { | |
const currentNode = this.head, | |
length = this._length, | |
count = 1, | |
message = {failure: 'Failure: non-existent node in this list.'}; | |
if (length === 0 || position < 1 || position > length) { | |
throw new Error(message.failure); | |
} | |
while (count < position) { | |
currentNode = currentNode.next; | |
count++; | |
} | |
return currentNode; | |
}; | |
DoublyList.prototype.remove = function(position) { | |
const currentNode = this.head, | |
length = this._length, | |
count = 1, | |
message = {failure: 'Error: non-existent node in this list.'}, | |
beforeNodeToDelete = null, | |
nodeToDelete = null, | |
deletedNode = null; | |
if (length === 0 || position < 1 || position > length) { | |
throw new Error(message.failure); | |
} | |
if (position === 1) { | |
this.head = currentNode.next; | |
if (!this.head) { | |
this.head.previous = null; | |
} else { | |
this.tail = null; | |
} | |
} else if (position === this._length) { | |
this.tail = this.tail.previous; | |
this.tail.next = null; | |
} else { | |
while (count < position) { | |
currentNode = currentNode.next; | |
count++; | |
} | |
beforeNodeToDelete = currentNode.previous; | |
nodeToDelete = currentNode; | |
afterNodeToDelete = currentNode.next; | |
beforeNodeToDelete.next = afterNodeToDelete; | |
afterNodeToDelete.previous = beforeNodeToDelete; | |
deletedNode = nodeToDelete; | |
nodeToDelete = null; | |
} | |
this._length--; | |
return message.success; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment