npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zerodep/struct-linkedlist

v2.5.11

Published

A factory that returns an optionally-typed, iterable Linked List data structure.

Downloads

98

Readme

@zerodep/struct-linkedlist

version language types license

CodeFactor Known Vulnerabilities

OpenSSF Best Practices

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