@zerodep/struct-linkedlist
TypeScript icon, indicating that this package has built-in type declarations

2.5.7 • Public • Published

@zerodep/struct-linkedlist

version language types license

CodeFactor Known Vulnerabilities

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"]

Package Sidebar

Install

npm i @zerodep/struct-linkedlist

Homepage

zerodep.app

Weekly Downloads

2

Version

2.5.7

License

MIT

Unpacked Size

9.26 kB

Total Files

7

Last publish

Collaborators

  • cdepage