node-dll
DLL(Doubly linked list) implementation for javascript projects
Introduction
Doubly linked list(DLL) is inevitable in certain situation where performance is preferred while slicing an array (maybe there are more to it, but this is mostly my PoV).
So this library was created with a simple, yet powerful interface for interacting with the underlying DLL
schema.
This library is written in typescript for robust APIs and type support.
Every aspect was desgined and thought carefully to minimize introducing new bugs.
Documentation
API method name has been carefully chosen to nearly match Array methods, such that learning curve is least and adaptability is higher. But a few methods couldn't have had a different name and we had to convince ourselves with those method name.
Installation
npm i @akashbabu/node-dll -S
Sample usage
import {DLL, DLLItem} from '@akashbabu/node-dll';
const dll = new DLL<string>();
const dllItem: DLLItem<string> = dll.push('foo');
console.log(dllItem.data)
console.log(dll.length)
dll.remove(dllItem)
console.log(dll.length)
dll.push('foo')
dll.unshift('bar')
const headItem = dll.head
console.log(headItem.data)
const headData = dll.shift()
console.log(headData)
console.log(dll.head.data)
dll.clear()
console.log(dll.length)
const firstItem = dll.push('first')
dll.push('third')
dll.appendAfter(firstItem, 'second')
dll.forEach((item, i) => {
console.log(`${i + 1}) ${item}`);
})
console.log(dll.map((item, i) => `${i + 1}) ${item}`));
API - DLL
new DLL():
This creates a new instance of DLL
.
T -> Denotes the type of data being saved in DLL
.head: DLLItem | null
Returns the first item in the list
.tail: DLLItem | null
Returns the last item in the list
.length: number
Returns the length of the list
.shift(): T | undefined
Removes and returns the first item in the list. Returns undefined
if the list is empty
.unshift(data: T): void
Adds the given item to the head of DLL (same as Array.unshift logic)
.forEach(cb: (data: T, i: number) => void): void
Iterates through the entire DLL
.map(cb: (data: T, i: number) => U): U[]
Iterates through the entire DLL and returns the resultant array.
U -> Denotes the return type of cb
(callback)
.push(data: T): DLLItem
Adds the given item to the tail of DLL and returns the added item
.appendAfter(dllItem: DLLItem, data: T): DLLItem
Adds the given data after the given dllItem in DLL and returns the added item
.remove(dllItem: DLLItem): boolean
Removes the given item from DLL and returns true if the removal was successful
.clear()
Removes all the items in the DLL
API - DLLItem
DLLItem has a readonly access to prev
and next
properties, this is to ensure that the users doesn't change them unintentionally (which can mess up with the entire DLL) and hence less surface for bugs.
.data
Set / get data value on the DLLItem via this property
.prev
Get prev value on the DLLItem via this property
.next
Get next value on the DLLItem via this property
Contribution
All contributions are welcome!!!
But before raising any issue, please check the issue-tracker in github if there is any matching issues already in the pipeline, if not then please go ahead and raise your own.
PR Guidelines:
- Make sure to include corresponding test cases
- Be generous on code comments
- Write documentation if necessary
- Wait for your approval 😜
Licence
MIT