|
| 1 | +import DoublyLinkedListNode from './DoublyLinkedListNode'; |
| 2 | +import Comparator from './../../utils/comparator/Comparator'; |
| 3 | + |
| 4 | +export default class DoublyLinkedList { |
| 5 | + /** |
| 6 | + * @param {Function} [comparatorFunction] |
| 7 | + */ |
| 8 | + constructor(comparatorFunction) { |
| 9 | + /** @var DoublyLinkedListNode */ |
| 10 | + this.head = null; |
| 11 | + |
| 12 | + /** @var DoublyLinkedListNode */ |
| 13 | + this.tail = null; |
| 14 | + |
| 15 | + this.compare = new Comparator(comparatorFunction); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @param {*} value |
| 20 | + * @return {DoublyLinkedList} |
| 21 | + */ |
| 22 | + prepend(value) { |
| 23 | + // Make new node to be a head. |
| 24 | + const newNode = new DoublyLinkedListNode(value, this.head); |
| 25 | + |
| 26 | + // If there is head, then it won't be head anymore |
| 27 | + // Therefore, make its previous reference to be new node (new head) |
| 28 | + // Then mark the new node as head |
| 29 | + if (this.head) { |
| 30 | + this.head.previous = newNode; |
| 31 | + } |
| 32 | + this.head = newNode; |
| 33 | + |
| 34 | + // If there is no tail yet let's make new node a tail. |
| 35 | + if (!this.tail) { |
| 36 | + this.tail = newNode; |
| 37 | + } |
| 38 | + |
| 39 | + return this; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @param {*} value |
| 44 | + * @return {DoublyLinkedList} |
| 45 | + */ |
| 46 | + append(value) { |
| 47 | + const newNode = new DoublyLinkedListNode(value); |
| 48 | + |
| 49 | + // If there is no head yet let's make new node a head. |
| 50 | + if (!this.head) { |
| 51 | + this.head = newNode; |
| 52 | + this.tail = newNode; |
| 53 | + |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + // Attach new node to the end of linked list. |
| 58 | + this.tail.next = newNode; |
| 59 | + |
| 60 | + // Attach current tail to the new node's previous reference |
| 61 | + newNode.previous = this.tail; |
| 62 | + |
| 63 | + // Set new node to be the tail of linked list. |
| 64 | + this.tail = newNode; |
| 65 | + |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param {*} value |
| 71 | + * @return {DoublyLinkedListNode} |
| 72 | + */ |
| 73 | + delete(value) { |
| 74 | + if (!this.head) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + let deletedNode = null; |
| 79 | + let currentNode = this.head; |
| 80 | + |
| 81 | + do { |
| 82 | + if (this.compare.equal(currentNode.value, value)) { |
| 83 | + deletedNode = currentNode; |
| 84 | + |
| 85 | + if (deletedNode === this.head) { |
| 86 | + // set head to second node, which will become new head |
| 87 | + this.head = deletedNode.next; |
| 88 | + |
| 89 | + // set new head's previous to null |
| 90 | + if (this.head) { |
| 91 | + this.head.previous = null; |
| 92 | + } |
| 93 | + |
| 94 | + // If all the nodes in list has same value that is passed as argument |
| 95 | + // then all nodes will get deleted, therefore tail needs to be updated |
| 96 | + if (deletedNode === this.tail) { |
| 97 | + this.tail = null; |
| 98 | + } |
| 99 | + } else if (deletedNode === this.tail) { |
| 100 | + // set tail to second last node, which will become new tail |
| 101 | + this.tail = deletedNode.previous; |
| 102 | + this.tail.next = null; |
| 103 | + } else { |
| 104 | + const previousNode = deletedNode.previous; |
| 105 | + const nextNode = deletedNode.next; |
| 106 | + previousNode.next = nextNode; |
| 107 | + nextNode.previous = previousNode; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + currentNode = currentNode.next; |
| 112 | + } while (currentNode); |
| 113 | + |
| 114 | + return deletedNode; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @param {Object} findParams |
| 119 | + * @param {*} findParams.value |
| 120 | + * @param {function} [findParams.callback] |
| 121 | + * @return {DoublyLinkedListNode} |
| 122 | + */ |
| 123 | + find({ value = undefined, callback = undefined }) { |
| 124 | + if (!this.head) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + let currentNode = this.head; |
| 129 | + |
| 130 | + while (currentNode) { |
| 131 | + // If callback is specified then try to find node by callback. |
| 132 | + if (callback && callback(currentNode.value)) { |
| 133 | + return currentNode; |
| 134 | + } |
| 135 | + |
| 136 | + // If value is specified then try to compare by value.. |
| 137 | + if (value !== undefined && this.compare.equal(currentNode.value, value)) { |
| 138 | + return currentNode; |
| 139 | + } |
| 140 | + |
| 141 | + currentNode = currentNode.next; |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * @return {DoublyLinkedListNode} |
| 149 | + */ |
| 150 | + deleteTail() { |
| 151 | + if (!this.tail) { |
| 152 | + return null; |
| 153 | + } else if (this.head === this.tail) { |
| 154 | + const deletedTail = this.tail; |
| 155 | + this.head = null; |
| 156 | + this.tail = null; |
| 157 | + |
| 158 | + return deletedTail; |
| 159 | + } |
| 160 | + |
| 161 | + const deletedTail = this.tail; |
| 162 | + this.tail = this.tail.previous; |
| 163 | + this.tail.next = null; |
| 164 | + |
| 165 | + return deletedTail; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @return {DoublyLinkedListNode} |
| 170 | + */ |
| 171 | + deleteHead() { |
| 172 | + if (!this.head) { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + const deletedHead = this.head; |
| 177 | + |
| 178 | + if (this.head.next) { |
| 179 | + this.head = this.head.next; |
| 180 | + this.head.previous = null; |
| 181 | + } else { |
| 182 | + this.head = null; |
| 183 | + this.tail = null; |
| 184 | + } |
| 185 | + |
| 186 | + return deletedHead; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @return {DoublyLinkedListNode[]} |
| 191 | + */ |
| 192 | + toArray() { |
| 193 | + const nodes = []; |
| 194 | + |
| 195 | + let currentNode = this.head; |
| 196 | + while (currentNode) { |
| 197 | + nodes.push(currentNode); |
| 198 | + currentNode = currentNode.next; |
| 199 | + } |
| 200 | + |
| 201 | + return nodes; |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * @param {function} [callback] |
| 206 | + * @return {string} |
| 207 | + */ |
| 208 | + toString(callback) { |
| 209 | + return this.toArray().map(node => node.toString(callback)).toString(); |
| 210 | + } |
| 211 | +} |
0 commit comments