Package Exports
- @zerodep/struct-linkedlist
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.
import { structLinkedListFactory } from '@zerodep/struct-linkedlist';
// or
const { structLinkedListFactory } = require('@zerodep/struct-linkedlist');
Use Case
A linked list may contain strings, numbers, objects or arrays. This example uses strings for simplicity.
const ll = structLinkedListFactory();
// append items to the linked list (to the end)
ll.append('c');
ll.append('d');
// prepend items to the linked list (at the start)
ll.prepend('b');
ll.prepend('a');
ll.size(); // 4
ll.toArray(); // ["a", "b", "c", "d"]
const head = ll.getHead(); // { data: "a", prev: null, next: [Object] }
const tail = ll.getTail(); // { data: "c", prev: [Object], next: null }
console.log(head.next.data); // "b"
console.log(tail.prev.data); // "c"
ll.insertBefore(tail, 'c2');
ll.insertAfter(head, 'a2');
ll.toArray(); // ["a", "a2", "b", "c", "c2", "d"]
ll.deleteHead();
ll.deleteTail();
const newHead = ll.getHead(); // { data: "a2", prev: null, next: [Object] }
const nextNode = newHead.next; // { data: "b", prev: [Object], next: [Object] }
ll.deleteNode(nextNode);
ll.toArray(); // ["a2", "c", "c2"]
// iterable
const values = [];
for (const val of ll) {
array.push(val);
}
console.log(values); // ["a2", "c", "c2"]