@datastructures-js/doubly-linked-list
Advanced tools
Comparing version 1.0.1 to 2.0.0
306
index.js
@@ -1,305 +0,3 @@ | ||
/** | ||
* datastructures-js/doubly-linked-list | ||
* @copyright 2018 Eyas Ranjous <eyas.ranjous@gmail.com> | ||
* @license MIT | ||
*/ | ||
const { DoublyLinkedList } = require('@datastructures-js/linked-list'); | ||
/** | ||
* Doubly Linked List Node | ||
* @function | ||
*/ | ||
const node = (val, pre, nex) => { | ||
let value = val; | ||
let prev = pre; | ||
let next = nex; | ||
/** | ||
* @param {string|number} v | ||
*/ | ||
const setValue = (v) => { | ||
value = v; | ||
}; | ||
/** | ||
* @return {string|number} | ||
*/ | ||
const getValue = () => value; | ||
/** | ||
* @param {object} - node | ||
*/ | ||
const setNext = (n) => { | ||
next = n; | ||
}; | ||
/** | ||
* @return {object} - node | ||
*/ | ||
const getNext = () => next || null; | ||
/** | ||
* @param {object} - node | ||
*/ | ||
const setPrev = (n) => { | ||
prev = n; | ||
}; | ||
/** | ||
* @return {object} - node | ||
*/ | ||
const getPrev = () => prev || null; | ||
return { | ||
setValue, | ||
getValue, | ||
setPrev, | ||
getPrev, | ||
setNext, | ||
getNext | ||
}; | ||
}; | ||
/** | ||
* Doubly Linked List | ||
* @function | ||
*/ | ||
const doublyLinkedList = () => { | ||
let headNode = null; | ||
let tailNode = null; | ||
let nodesCount = 0; | ||
/** | ||
* @returns {object} - node | ||
*/ | ||
const head = () => headNode; | ||
/** | ||
* @returns {object} - node | ||
*/ | ||
const tail = () => tailNode; | ||
/** | ||
* @returns {number} | ||
*/ | ||
const count = () => nodesCount; | ||
/** | ||
* @param {string|number} value | ||
* @returns {object} - node | ||
*/ | ||
const find = (value) => { | ||
let current = headNode; | ||
while (current !== null) { | ||
if (current.getValue() === value) { | ||
return current; | ||
} | ||
current = current.getNext(); | ||
} | ||
return current; | ||
}; | ||
/** | ||
* @param {string|number} value | ||
*/ | ||
const addFirst = (value) => { | ||
if (headNode === null) { | ||
headNode = node(value); | ||
tailNode = headNode; | ||
} else { | ||
const n = node(value, null, headNode); | ||
headNode.setPrev(n); | ||
headNode = n; | ||
} | ||
nodesCount += 1; | ||
}; | ||
/** | ||
* @param {string|number} value | ||
*/ | ||
const addLast = (value) => { | ||
if (headNode === null) { | ||
headNode = node(value); | ||
tailNode = headNode; | ||
} else { | ||
const n = node(value, tailNode, null); | ||
tailNode.setNext(n); | ||
tailNode = n; | ||
} | ||
nodesCount += 1; | ||
}; | ||
/** | ||
* adds a new node after an existing node | ||
* @param {string|number} value | ||
* @param {string|number} newValue | ||
* @throws {Error} | ||
*/ | ||
const addAfter = (value, newValue) => { | ||
let current = headNode; | ||
while (current !== null) { | ||
if (current.getValue() === value) { | ||
if (current.getNext() === null) { | ||
addLast(newValue); | ||
} else { | ||
const n = node(newValue, current, current.getNext()); | ||
current.getNext().setPrev(n); | ||
current.setNext(n); | ||
nodesCount += 1; | ||
} | ||
break; | ||
} else { | ||
current = current.getNext(); | ||
} | ||
} | ||
if (current === null) { | ||
throw new Error(`node ${value} not found`); | ||
} | ||
}; | ||
/** | ||
* @public | ||
* adds a new node before an existing node | ||
* @param {(string|number)} value | ||
* @param {(string|number)} newValue | ||
* @throws {Error} | ||
*/ | ||
const addBefore = (value, newValue) => { | ||
let current = tailNode; | ||
while (current !== null) { | ||
if (current.getValue() === value) { | ||
if (current.getPrev() === null) { | ||
addFirst(newValue); | ||
} else { | ||
const n = node(newValue, current.getPrev(), current); | ||
current.getPrev().setNext(n); | ||
current.setPrev(n); | ||
nodesCount += 1; | ||
} | ||
break; | ||
} else { | ||
current = current.getPrev(); | ||
} | ||
} | ||
if (current === null) { | ||
throw new Error(`node ${value} not found`); | ||
} | ||
}; | ||
/** | ||
* removes the first node | ||
*/ | ||
const removeFirst = () => { | ||
if (headNode !== null) { | ||
if (headNode.getNext() === null) { | ||
headNode = null; | ||
tailNode = null; | ||
} else { | ||
headNode = headNode.getNext(); | ||
headNode.setPrev(null); | ||
} | ||
nodesCount -= 1; | ||
} | ||
}; | ||
/** | ||
* removes the last node | ||
*/ | ||
const removeLast = () => { | ||
if (tailNode !== null) { | ||
if (tailNode.getPrev() === null) { | ||
headNode = null; | ||
tailNode = null; | ||
} else { | ||
tailNode = tailNode.getPrev(); | ||
tailNode.setNext(null); | ||
} | ||
nodesCount -= 1; | ||
} | ||
}; | ||
/** | ||
* removes a node by its value | ||
* @param {(string|number)} value | ||
*/ | ||
const remove = (value) => { | ||
let current = headNode; | ||
while (current !== null) { | ||
if (current.getValue() === value) { | ||
if (current.getPrev() === null) { | ||
removeFirst(); | ||
} else { | ||
current.getPrev().setNext(current.getNext()); | ||
current.getNext().setPrev(current.getPrev()); | ||
nodesCount -= 1; | ||
} | ||
break; | ||
} else { | ||
current = current.getNext(); | ||
} | ||
} | ||
}; | ||
/** | ||
* traverse the doublyLinkedlist from a beginning to end | ||
* @param {function} cb - called with node in the linked list | ||
*/ | ||
const traverse = (cb) => { | ||
let current = headNode; | ||
while (current !== null) { | ||
cb(current); | ||
current = current.getNext(); | ||
} | ||
}; | ||
/** | ||
* traverse the doublyLinkedList from the end to start | ||
* @param {function} cb | ||
*/ | ||
const traverseBackward = (cb) => { | ||
let current = tailNode; | ||
while (current !== null) { | ||
cb(current); | ||
current = current.getPrev(); | ||
} | ||
}; | ||
/** | ||
* @returns {array} | ||
*/ | ||
const toArray = () => { | ||
const arr = []; | ||
traverse(n => arr.push(n.getValue())); | ||
return arr; | ||
}; | ||
/** | ||
* clears the linked list | ||
*/ | ||
const clear = () => { | ||
headNode = null; | ||
tailNode = null; | ||
nodesCount = 0; | ||
}; | ||
// doublyLinkedList API | ||
return { | ||
node, | ||
head, | ||
tail, | ||
count, | ||
find, | ||
addFirst, | ||
addLast, | ||
addBefore, | ||
addAfter, | ||
remove, | ||
removeFirst, | ||
removeLast, | ||
traverse, | ||
traverseBackward, | ||
toArray, | ||
clear | ||
}; | ||
}; | ||
module.exports = doublyLinkedList; | ||
module.exports = DoublyLinkedList; |
{ | ||
"name": "@datastructures-js/doubly-linked-list", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "doubly linked list implementation in javascript", | ||
@@ -35,3 +35,6 @@ "main": "index.js", | ||
"mocha": "^5.0.0" | ||
}, | ||
"dependencies": { | ||
"@datastructures-js/linked-list": "^2.0.2" | ||
} | ||
} |
165
README.md
@@ -7,164 +7,23 @@ # @datastrucures-js/doubly-linked-list | ||
node's data type: **number**, **string**, **boolean**, **null**, **undefined**. | ||
A wraper around https://github.com/datastructures-js/linked-list DoublyLinkedList | ||
<img width="552" alt="dll" src="https://user-images.githubusercontent.com/6517308/35762752-19b17df4-0862-11e8-8ce3-f940d83dde51.png"> | ||
## install | ||
```sh | ||
npm install --save @datastructures-js/doubly-linked-list | ||
``` | ||
## Usage | ||
## require | ||
```js | ||
const doublyLinkedListFn = require('@datastructures-js/doubly-linked-list'); | ||
const dll = doublyLinkedListFn(); | ||
const DoublyLinkedList = require('@datastructures-js/doubly-linked-list'); | ||
``` | ||
## API | ||
**.node(value)** | ||
creates a linked list node with a given value. The node object exposes the following functions: | ||
* **.setValue(value)** sets the value of the node. | ||
* **.getValue()** gets the value of the node | ||
* **.setNext(node)** sets the next linkedListNode object. | ||
* **.getNext()** gets the next linkedListNode object. | ||
* **.setPrev(node)** sets the previous linkedListNode object. | ||
* **.getPrev()** gets the previous linkedListNode object. | ||
```javascript | ||
const n = dll.node('new_node'); | ||
console.log(n.getValue()); // new_node | ||
console.log(n.getNext()); // null | ||
console.log(n.getPrev()); // null | ||
## import | ||
```js | ||
import DoublyLinkedList from '@datastructures-js/doubly-linked-list'; | ||
``` | ||
**.addFirst(value)** | ||
## API | ||
https://github.com/datastructures-js/linked-list#create-a-list | ||
adds a node of the given value at the beginning of the list. | ||
```javascript | ||
dll.addFirst('n1'); | ||
``` | ||
**.addLast(value)** | ||
adds a node of the given value at the end of the list. | ||
```javascript | ||
dll.addLast('n4'); | ||
``` | ||
**.addAfter(value, newValue)** | ||
adds a node with a given value after an existing value's node. | ||
```javascript | ||
try { | ||
dll.addAfter('n1', 'n2'); | ||
dll.addAfter('n33', 'n3'); | ||
} | ||
catch (e) { | ||
console.log(e.message); // node n33 not found | ||
} | ||
``` | ||
**.addBefore(value, newValue)** | ||
adds a node with a given value before an existing value's node. | ||
```javascript | ||
try { | ||
dll.addBefore('n4', 'n3'); | ||
dll.addBefore('n33', 'n3'); | ||
} | ||
catch (e) { | ||
console.log(e.message); // node n33 not found | ||
} | ||
``` | ||
**.find(value)** | ||
finds a node by its value and returns a linked list node object. | ||
```javascript | ||
const n3 = dll.find('n3'); | ||
console.log(n3.getValue()); // n3 | ||
console.log(n3.getNext().getValue()); // n4 | ||
``` | ||
**.head()** | ||
returns the first dll node object in the list. | ||
```javascript | ||
const head = dll.head(); | ||
console.log(head.getValue()); // n1 | ||
``` | ||
**.tail()** | ||
returns the first dll node object in the list. | ||
```javascript | ||
const tail = dll.tail(); | ||
console.log(head.getValue()); // n1 | ||
``` | ||
**.traverse(cb)** | ||
traverse the dll from beginning to end and calls cb for each node | ||
```javascript | ||
dll.traverse((n) => { console.log(n.getValue()); }); | ||
// n1 | ||
// n2 | ||
// n3 | ||
// n4 | ||
``` | ||
**.traverseBackward(cb)** | ||
traverse the dll from end to beginning and calls cb for each node | ||
```javascript | ||
dll.traverseBackward((n) => { console.log(n.getValue()); }); | ||
// n4 | ||
// n3 | ||
// n2 | ||
// n1 | ||
``` | ||
**.remove(value)** | ||
remove the value's node - if exists - from the list. | ||
```javascript | ||
dll.remove('n3'); | ||
``` | ||
**.removeFirst()** | ||
removes the first node in the list. | ||
```javascript | ||
dll.removeFirst(); // n1 removed | ||
``` | ||
**.removeLast()** | ||
removes the last node in the list. | ||
```javascript | ||
dll.removeLast(); // n4 removed | ||
``` | ||
**.toArray()** | ||
converts the dll to an array | ||
```javascript | ||
console.log(dll.toArray()); | ||
// ['n1', 'n2', 'n3', 'n4'] | ||
``` | ||
**.count()** | ||
returns nodes' count in the list. | ||
```javascript | ||
console.log(dll.count()); // 1 | ||
``` | ||
**.clear()** | ||
removes all nodes from the list. | ||
```javascript | ||
dll.clear(); | ||
console.log(dll.head()); // null | ||
console.log(dll.count()); // 0 | ||
``` | ||
## Build | ||
@@ -171,0 +30,0 @@ ``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3738
1
5
2
35
2
+ Added@datastructures-js/linked-list@2.0.3(transitive)