@zerodep/struct-linkedlist
v2.5.11
Published
A factory that returns an optionally-typed, iterable Linked List data structure.
Downloads
17
Maintainers
Readme
@zerodep/struct-linkedlist
A factory function that returns an optionally-typed, iterable Doubly Linked List data structure instance.
Full documentation is available at the zerodep.app site.
Examples
All @zerodep packages support both ESM and CJS formats, each complete with Typescript typings.
Signature
declare const structLinkedListFactory: <T = unknown>(data?: T[]) => LinkedList<T>;
interface LinkedList<T> {
size: () => number;
getHead: () => LinkedListNode<T> | null;
getTail: () => LinkedListNode<T> | null;
prepend: (data: T) => void;
append: (data: T) => void;
find: (comparator: (data: T) => boolean) => Node<T> | null;
insertBefore: (node: LinkedListNode<T>, data: T) => void;
insertAfter: (node: LinkedListNode<T>, data: T) => void;
deleteNode: (node: LinkedListNode<T>) => void;
deleteHead: () => void;
deleteTail: () => void;
clear: () => void;
toArray: () => T[];
toArrayReverse: () => T[];
}
interface LinkedListNode<T> {
data: T;
prev: LinkedListNode<T> | null;
next: LinkedListNode<T> | null;
}
Factory Parameters
The structLinkedlistFactory
function has the following parameters:
- data - an optional array of data items with which to populate the linked list, the first item in the array will be the head item
Linked List Methods
The structLinkedlistFactory
returns an object with the following methods:
- size() - how many nodes/items are in the linked list
- getHead() - returns the head node of the list
- getTail() - returns the tail node of the list
- prepend(data) - add a new node to the front of the list
- append(item) - add a new node to the end of the list
- find(comparator) - finds a node in the list
- insertBefore(node, item) - inserts a new node before specified node
- insertAfter(node, item) - inserts a new node after specified node
- deleteNode(node) - deletes the specified node
- deleteHead() - deletes the head node
- deleteTail() - deletes the tail node
- clear() - removes all nodes from the linked list
- toArray() - converts the linkedlist to an array suitable for serialization, with the first item in the list being the head of the list
Linked List Node Properties
The getHead()
, getTail()
and find()
methods return nodes which have the following properties:
- data - the value of the node, may be anything (string, number, object, etc...)
- prev - the reference/pointer to the previous node in the list
- next - the reference/pointer to the next node in the list
Examples
// ESM
import { structLinkedListFactory } from '@zerodep/struct-linkedlist';
// CJS
const { structLinkedListFactory } = require('@zerodep/struct-linkedlist');
Adding, Removing and Getting Nodes
const linkedlist = structLinkedlistFactory<string>(['c', 'd']);
// add items to the start of the list
linkedlist.prepend('b');
linkedlist.prepend('a');
// add items to the end of the list
linkedlist.append('e');
linkedlist.append('f');
linkedlist.size(); // 6
linkedlist.toArray(); // ["a", "b", "c", "d", "e", "f"]
const head = linkedlist.getHead(); // { "data": "a", "prev": null, "next": [Object] }
const tail = linkedlist.getTail(); // { "data": "f", "prev": [Object], "next": null }
const thirdItem = head.next.next; // { "data": "c", "prev": [Object], "next": [Object] }
linkedList.insertBefore(thirdItem, 'pre-c');
linkedList.insertAfter(thirdItem, 'post-c');
linkedList.deleteNode(thirdItem);
linkedlist.toArray(); // ["a", "b", "pre-c", "post-c", "d", "e", "f"]
linkedList.deleteHead();
linkedList.deleteTail();
const newHead = linkedList.getHead(); // { "data": "b", "prev": null, "next": [Object] }
const newTail = linkedList.getTail(); // { "data": "e", "prev": [Object], "next": null }
linkedList.clear();
linkedList.toArray(); // []
Searching the List
const linkedlist = structLinkedlistFactory<string>(['aaa1', 'aaa2', 'aaa3', 'aaa4', 'aaa5']);
// comparators must return a boolean
const comparator = (val: string) => val.endsWith('3');
const node = linkedlist.find(comparator); // { "data": "aaa3", "prev": [Object], "next": [Object] }
Iterating the List
const linkedlist = structLinkedlistFactory<string>(['a', 'b', 'c', 'd', 'e', 'f']);
const result: array[] = [];
for (const value of linkedList) {
result.push(value);
}
console.log(result); // ["a", "b", "c", "d", "e", "f"]
Walking the List
const linkedlist = structLinkedlistFactory<string>(['a', 'b', 'c', 'd', 'e', 'f']);
// going forwards
const head: LinkedListNode = linkedlist.getHead(); // { "data": "a", "prev": null, "next": [Object] }
const fourthItem = head?.next?.next?.next; // { "data": "d", "prev": [Object], "next": [Object] }
// going backwards
const tail: LinkedListNode = linkedlist.getTail(); // { "data": "a", "prev": [Object] , "next": null}
const fourthPreviousItem = tail?.prev?.prev?.prev?.prev; // { "data": "b", "prev": [Object], "next": [Object] }
ZeroDep Advantages
- Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases node_modules folder size
- ESM & CJS - supports both ECMAScript modules and common JavaScript exports
- Tree Shakable - built to be fully tree shakable ensuring your packages are the smallest possible size
- Fully Typed - typescript definitions are provided/built-in to every package for a superior developer experience
- Semantically Named - package and method names are easy to grok, remember, use, and read
- Documented - actually useful documentation with examples at zerodep.app
- Intelligently Packaged - multiple npm packages of different sizes available allowing a menu or a-la-carte composition of capabilities
- 100% Tested - all methods and packages are fully unit tested
- Predictably Versioned - semantically versioned for peace-of-mind upgrading, valuable changelogs for understand changes
- MIT Licensed - permissively licensed for maximum usability