|
| 1 | +importDoublyLinkedListNodefrom'./DoublyLinkedListNode'; |
| 2 | +importComparatorfrom'./../../utils/comparator/Comparator'; |
| 3 | + |
| 4 | +exportdefaultclassDoublyLinkedList{ |
| 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=newComparator(comparatorFunction); |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + *@param {*} value |
| 20 | + *@return {DoublyLinkedList} |
| 21 | + */ |
| 22 | +prepend(value){ |
| 23 | +// Make new node to be a head. |
| 24 | +constnewNode=newDoublyLinkedListNode(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 | +returnthis; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + *@param {*} value |
| 44 | + *@return {DoublyLinkedList} |
| 45 | + */ |
| 46 | +append(value){ |
| 47 | +constnewNode=newDoublyLinkedListNode(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 | +returnthis; |
| 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 | +returnthis; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + *@param {*} value |
| 71 | + *@return {DoublyLinkedListNode} |
| 72 | + */ |
| 73 | +delete(value){ |
| 74 | +if(!this.head){ |
| 75 | +returnnull; |
| 76 | +} |
| 77 | + |
| 78 | +letdeletedNode=null; |
| 79 | +letcurrentNode=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 | +}elseif(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 | +constpreviousNode=deletedNode.previous; |
| 105 | +constnextNode=deletedNode.next; |
| 106 | +previousNode.next=nextNode; |
| 107 | +nextNode.previous=previousNode; |
| 108 | +} |
| 109 | +} |
| 110 | + |
| 111 | +currentNode=currentNode.next; |
| 112 | +}while(currentNode); |
| 113 | + |
| 114 | +returndeletedNode; |
| 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 | +returnnull; |
| 126 | +} |
| 127 | + |
| 128 | +letcurrentNode=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 | +returncurrentNode; |
| 134 | +} |
| 135 | + |
| 136 | +// If value is specified then try to compare by value.. |
| 137 | +if(value!==undefined&&this.compare.equal(currentNode.value,value)){ |
| 138 | +returncurrentNode; |
| 139 | +} |
| 140 | + |
| 141 | +currentNode=currentNode.next; |
| 142 | +} |
| 143 | + |
| 144 | +returnnull; |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + *@return {DoublyLinkedListNode} |
| 149 | + */ |
| 150 | +deleteTail(){ |
| 151 | +if(!this.tail){ |
| 152 | +returnnull; |
| 153 | +}elseif(this.head===this.tail){ |
| 154 | +constdeletedTail=this.tail; |
| 155 | +this.head=null; |
| 156 | +this.tail=null; |
| 157 | + |
| 158 | +returndeletedTail; |
| 159 | +} |
| 160 | + |
| 161 | +constdeletedTail=this.tail; |
| 162 | +this.tail=this.tail.previous; |
| 163 | +this.tail.next=null; |
| 164 | + |
| 165 | +returndeletedTail; |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + *@return {DoublyLinkedListNode} |
| 170 | + */ |
| 171 | +deleteHead(){ |
| 172 | +if(!this.head){ |
| 173 | +returnnull; |
| 174 | +} |
| 175 | + |
| 176 | +constdeletedHead=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 | +returndeletedHead; |
| 187 | +} |
| 188 | + |
| 189 | +/** |
| 190 | + *@return {DoublyLinkedListNode[]} |
| 191 | + */ |
| 192 | +toArray(){ |
| 193 | +constnodes=[]; |
| 194 | + |
| 195 | +letcurrentNode=this.head; |
| 196 | +while(currentNode){ |
| 197 | +nodes.push(currentNode); |
| 198 | +currentNode=currentNode.next; |
| 199 | +} |
| 200 | + |
| 201 | +returnnodes; |
| 202 | +} |
| 203 | + |
| 204 | +/** |
| 205 | + *@param {function} [callback] |
| 206 | + *@return {string} |
| 207 | + */ |
| 208 | +toString(callback){ |
| 209 | +returnthis.toArray().map(node=>node.toString(callback)).toString(); |
| 210 | +} |
| 211 | +} |