2.5.7 • Published 2 months ago

@zerodep/struct-linkedlist v2.5.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

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

2 months ago

2.5.6

2 months ago

2.5.5

2 months ago

2.5.4

2 months ago

2.5.3

7 months ago

2.5.2

7 months ago

2.5.1

8 months ago

2.5.0

8 months ago