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

@sandro-e-martinez/linked-list

v2.0.0

Published

Linked List implemented with typescript

Downloads

1

Readme

linked-list

A TypeScript implementation of a generic singly-linked list.

Contents

Installation

To install this package, run:

Installation

To install this package, run:

npm install @sandro-e-martinez/linked-list

Usage

First, import the LinkedList class:

import { LinkedList } from '@sandro-e-martinez/linked-list';
Creating a LinkedList

Create a new linked list:

const list = new LinkedList<number>();
Appending Elements

Append elements to the end of the list:

list.append(1);
list.append(2);
list.append(3);
Prepending Elements

Prepend elements to the beginning of the list:

list.prepend(0);
Deleting Elements

Delete the first node with a given value:

list.delete(2);
Looking Up By Position

Retrieve a node based on its position (1-based index):

let nodeAtThirdPosition = list.lookup(3);
Finding the Position of a Value

Find the position (1-based index) of the first occurrence of a value:

let positionOfValue2 = list.indexOf(2); // should return 2 if the list has the value 2 at the second position
Inserting Elements at a Specific Position

Insert a value at a specific position (1-based index):

list.insert(2, 1.5); // inserts the value 1.5 at the second position

Converting to Array

Convert the list to an array:

const arr = list.toArray();

Using the reverseLinkedList Helper

For more advanced reversing tasks, like reversing only a specific part of the list, you can use the reverseLinkedList helper function. First, import the helper:

import { reverseLinkedList } from '@sandro-e-martinez/linked-list/helpers/reverse';

const list = new LinkedList<number>();
list.append(1);
list.append(2);
list.append(3);
list.append(4);
list.append(5);

// Original list: [1, 2, 3, 4, 5]
list = new LinkedList(reverseLinkedList(list.head)); // Reverse entire list
// Output list [5, 4, 3, 2, 1]

// To reverse a sublist from the 2nd to 4th node (1-based index)
list = reverseLinkedList(list.head, 2, 4);
// Output list [1, 4, 3, 2, 5]

API

Core Methods

These are the fundamental methods for manipulating the singly-linked list, including appending, prepending, and deleting nodes.

append(value: T): void

Appends a new node with the given value to the end of the list.

prepend(value: T): void

Prepends a new node with the given value to the beginning of the list.

delete(value: T): void

Deletes the first node with the given value from the list.

lookup(position: number): Node<T> | null

Retrieves a node from the linked list based on its position.

indexOf(value: T): number

Returns the position of the first node with the given value.

insert(position: number, value: T): void

Inserts a value at a specific position.

Auxiliary Methods
toArray(): T[]

Returns an array containing all the elements of the linked list.

Helper Methods

These methods provide additional capabilities for manipulating your linked list, such as reversing it entirely or partially.

reverse(from?: number, to?: number): void

Reverses the entire list if no parameters are provided. Reverses a sublist from position from to position to if both are provided.