@datastructures-js/linked-list
Advanced tools
Comparing version 3.0.3 to 4.0.0
@@ -9,2 +9,8 @@ # Changelog | ||
## [4.0.0] - 2021-02-16 | ||
### Changed | ||
- `.removeFirst()`, `.removeLast()`, `.removeAt`, `.removeEach` now return the removed nodes. | ||
## [3.0.3] - 2021-01-30 | ||
@@ -11,0 +17,0 @@ |
{ | ||
"name": "@datastructures-js/linked-list", | ||
"version": "3.0.3", | ||
"version": "4.0.0", | ||
"description": "a javascript implementation of LinkedList & DoublyLinkedList", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -445,3 +445,3 @@ # @datastrucures-js/linked-list | ||
<td align="center"> | ||
boolean | ||
LinkedListNode | DoublyLinkedListNode | ||
</td> | ||
@@ -455,9 +455,9 @@ <td align="center"> | ||
```js | ||
linkedList.removeFirst(); // true | ||
linkedList.removeFirst(); | ||
doublyLinkedList.removeFirst(); // true | ||
doublyLinkedList.removeFirst(); | ||
``` | ||
### .removeLast() | ||
removes the last node in the list. | ||
removes and returns the last node in the list. | ||
@@ -471,3 +471,3 @@ <table> | ||
<td align="center"> | ||
boolean | ||
LinkedListNode | DoublyLinkedListNode | ||
</td> | ||
@@ -483,9 +483,9 @@ <td> | ||
```js | ||
linkedList.removeLast(); // true | ||
linkedList.removeLast(); | ||
doublyLinkedList.removeLast(); // true | ||
doublyLinkedList.removeLast(); | ||
``` | ||
### .removeAt(position) | ||
removes a node at a specific position. First (head) node is at position 0. | ||
removes and returns the node at a specific position. First (head) node is at position 0. | ||
@@ -503,3 +503,3 @@ <table> | ||
<td align="center"> | ||
boolean | ||
LinkedListNode | DoublyLinkedListNode | ||
</td> | ||
@@ -513,9 +513,9 @@ <td align="center"> | ||
```js | ||
linkedList.removeAt(1); // true | ||
linkedList.removeAt(1); | ||
doublyLinkedList.removeAt(1); // true | ||
doublyLinkedList.removeAt(1); | ||
``` | ||
### .removeEach(cb) | ||
Loop on the linked list from beginning to end, removes the nodes that returns true from the callback. | ||
Loop on the linked list from beginning to end, removes the nodes that returns a list of the removed nodes. | ||
@@ -533,3 +533,3 @@ <table> | ||
<td align="center"> | ||
number <i>(number of removed nodes)</i> | ||
array<LinkedListNode | DoublyLinkedListNode> | ||
</td> | ||
@@ -543,6 +543,6 @@ <td align="center"> | ||
```js | ||
linkedList.removeEach((node, position) => node.getValue() > 1); // 1 | ||
linkedList.removeEach((node, position) => node.getValue() > 1); | ||
console.log(linkedList.toArray()); // [1] | ||
doublyLinkedList.removeEach((node, position) => node.getValue() > 1); // 1 | ||
doublyLinkedList.removeEach((node, position) => node.getValue() > 1); | ||
console.log(doublyLinkedList.toArray()); // [1] | ||
@@ -549,0 +549,0 @@ ``` |
@@ -105,7 +105,8 @@ /** | ||
* @public | ||
* @returns {boolean} | ||
* @returns {DoublyLinkedListNode|null} | ||
*/ | ||
removeFirst() { | ||
if (this._head === null) return false; | ||
if (this._head === null) return null; | ||
const removed = this._head; | ||
if (this._head.getNext() === null) { | ||
@@ -120,3 +121,3 @@ this._head = null; | ||
this._count -= 1; | ||
return true; | ||
return removed.setNext(null).setPrev(null); | ||
} | ||
@@ -127,7 +128,8 @@ | ||
* @public | ||
* @returns {boolean} | ||
* @returns {DoublyLinkedListNode|null} | ||
*/ | ||
removeLast() { | ||
if (this._head === null) return false; | ||
if (this._head === null) return null; | ||
const removed = this._tail; | ||
if (this._head.getNext() === null) { | ||
@@ -142,3 +144,3 @@ this._head = null; | ||
this._count -= 1; | ||
return true; | ||
return removed.setNext(null).setPrev(null); | ||
} | ||
@@ -150,3 +152,3 @@ | ||
* @param {number} position | ||
* @returns {boolean} | ||
* @returns {DoublyLinkedListNode|null} | ||
*/ | ||
@@ -159,3 +161,3 @@ removeAt(position) { | ||
) { | ||
return false; | ||
return null; | ||
} | ||
@@ -178,6 +180,8 @@ | ||
const removed = prev.getNext(); | ||
prev.setNext(prev.getNext().getNext()); | ||
prev.getNext().setPrev(prev); | ||
this._count -= 1; | ||
return true; | ||
return removed.setNext(null).setPrev(null); | ||
} | ||
@@ -189,3 +193,3 @@ | ||
* @param {function} cb | ||
* @returns {number} count of removed nodes | ||
* @returns {array} count of removed nodes | ||
*/ | ||
@@ -197,3 +201,3 @@ removeEach(cb) { | ||
let removed = 0; | ||
const removed = []; | ||
let position = 0; | ||
@@ -206,6 +210,7 @@ let prev = null; | ||
if (prev === null) { | ||
this.removeFirst(); | ||
removed.push(this.removeFirst()); | ||
} else if (prev.getNext() === null) { | ||
this.removeLast(); | ||
removed.push(this.removeLast()); | ||
} else { | ||
const removedNode = prev.getNext(); | ||
prev.setNext(prev.getNext().getNext()); | ||
@@ -215,5 +220,5 @@ if (prev.getNext() !== null) { | ||
} | ||
removed.push(removedNode.setNext(null).setPrev(null)); | ||
this._count -= 1; | ||
} | ||
removed += 1; | ||
} | ||
@@ -220,0 +225,0 @@ position += 1; |
@@ -44,15 +44,10 @@ /** | ||
const insertLastRecursive = (current) => { | ||
// not the last node, move to next | ||
if (current.getNext() instanceof LinkedListNode) { | ||
return insertLastRecursive(current.getNext()); | ||
} | ||
let current = this._head; | ||
while (current.getNext() instanceof LinkedListNode) { | ||
current = current.getNext(); | ||
} | ||
// arrived to last node, add new node | ||
current.setNext(new LinkedListNode(value)); | ||
this._count += 1; | ||
return this; | ||
}; | ||
return insertLastRecursive(this._head); | ||
current.setNext(new LinkedListNode(value)); | ||
this._count += 1; | ||
return this; | ||
} | ||
@@ -96,10 +91,11 @@ | ||
* @public | ||
* @returns {boolean} | ||
* @returns {LinkedListNode|null} | ||
*/ | ||
removeFirst() { | ||
if (this.isEmpty()) return false; | ||
if (this.isEmpty()) return null; | ||
const removed = this._head; | ||
this._head = this._head.getNext(); | ||
this._count -= 1; | ||
return true; | ||
return removed.setNext(null); | ||
} | ||
@@ -112,6 +108,6 @@ | ||
* @param {LinkedListNode} [current] - current node | ||
* @returns {boolean} | ||
* @returns {LinkedListNode|null} | ||
*/ | ||
removeLast(prev = null, current = this._head) { | ||
if (this.isEmpty()) return false; | ||
if (this.isEmpty()) return null; | ||
@@ -129,5 +125,6 @@ // not last node, move next | ||
// arrived to last node, remove it | ||
const removed = prev.getNext(); | ||
prev.setNext(null); | ||
this._count -= 1; | ||
return true; | ||
return removed.setNext(null); | ||
} | ||
@@ -139,3 +136,3 @@ | ||
* @param {function} cb - callback should return true for removed nodes. | ||
* @returns {number} count of removed nodes | ||
* @returns {array} removed nodes | ||
*/ | ||
@@ -147,3 +144,3 @@ removeEach(cb) { | ||
let removed = 0; | ||
const removedNodes = []; | ||
let position = 0; | ||
@@ -155,9 +152,11 @@ let prev = null; | ||
if (cb(current, position)) { | ||
let removed; | ||
if (prev === null) { | ||
this.removeFirst(); | ||
removedNodes.push(this.removeFirst()); | ||
} else { | ||
removed = prev.getNext(); | ||
prev.setNext(prev.getNext().getNext()); | ||
this._count -= 1; | ||
removedNodes.push(removed.setNext(null)); | ||
} | ||
removed += 1; | ||
} | ||
@@ -169,3 +168,3 @@ position += 1; | ||
return removed; | ||
return removedNodes; | ||
} | ||
@@ -177,3 +176,3 @@ | ||
* @param {number} position | ||
* @returns {boolean} | ||
* @returns {LinkedListNode|null} | ||
*/ | ||
@@ -186,3 +185,3 @@ removeAt(position) { | ||
) { | ||
return false; | ||
return null; | ||
} | ||
@@ -201,5 +200,6 @@ | ||
const removed = prev.getNext(); | ||
prev.setNext(prev.getNext().getNext()); | ||
this._count -= 1; | ||
return true; | ||
return removed.setNext(null); | ||
} | ||
@@ -206,0 +206,0 @@ |
33600
706