Last active
November 6, 2016 03:52
-
-
Save ZaynahGiti/4508082ae875ff265a5a5c602a4d0823 to your computer and use it in GitHub Desktop.
LinkedList created by ZaynahGiti - https://repl.it/EPbx/1
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 LinkedList() { | |
| this.head = null; | |
| this.tail = null; | |
| } | |
| function Node(value, next, prev) { | |
| this.value = value; | |
| this.next = next; | |
| this.prev = prev; | |
| } | |
| LinkedList.prototype.addToHead = function(value) { | |
| var newNode = new Node(value, this.head, null); | |
| if (this.head) this.head.prev = newNode; | |
| else this.tail = newNode; | |
| this.head = newNode; | |
| }; | |
| LinkedList.prototype.addToTail = function(value) { | |
| var newNode = new Node(value, null, this.tail); | |
| if (this.tail) this.tail.next = newNode; | |
| else this.head = newNode; | |
| this.tail = newNode; | |
| }; | |
| LinkedList.prototype.removeHead = function() { | |
| if (!this.head) return null; | |
| var val = this.head.value; | |
| this.head = this.head.next; | |
| if (this.head) this.head.prev = null; | |
| else this.tail = null; | |
| return val; | |
| }; | |
| LinkedList.prototype.removeTail = function() { | |
| if (!this.tail) return null; | |
| var val = this.tail.value; | |
| this.tail = this.tail.prev; | |
| if (this.tail) this.tail.next = null; | |
| else this.head = null; | |
| return val; | |
| }; | |
| LinkedList.prototype.search = function(searchValue) { | |
| var currentNode = this.head; | |
| while (currentNode) { | |
| if (currentNode.value === searchValue) return currentNode.value; | |
| currentNode = currentNode.next; | |
| } | |
| return null; | |
| }; | |
Author
ZaynahGiti
commented
Nov 6, 2016

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment