@dstl-js/linklist

1.0.0-beta.0 • Public • Published

@dstl-js/linklist

Download

You can download the source code directly from the CDN and use it:

You can also install it directly using the package manager:

npm i @dstl-js/linklist
pnpm add @dstl-js/linklist
yarn add @dstl-js/linklist

Usage

import { LinkList } from '@dstl-js/linklist';

const linklist = new LinkList<number>([1, 2, 3, 4, 5]);

linklist.unshift(6);
console.log(linklist.at(0)); // 6
console.log(linklist.tail); // 5

for (const [i, v] of linklist) console.log(v); // 6, 1, 2, 3, 4, 5

Iterable

The LinkList container is an iterable, you can use for...of loop, spread syntax, yield* keyword, and array deconstruction operate on container

Example

import { LinkList } from '@dstl-js/linklist';

const linklist = new LinkList<number>([1, 2, 3, 4, 5]);

// for...of
for (const [i, v] of linklist) {
  //...
}

// spread syntax
const arr = [...linklist]; // [1, 2, 3, 4, 5]

// yield*
function* fn() {
  yield* linklist;
}
const it = fn();
console.log(it.next().value); // 1

// array deconstruction
const [a, b, c, d, e] = linklist;
console.log(a, b, c, d, e); // 1, 2, 3, 4, 5

API

Constructor

  • new LinkList(iterable?: Iterable<T>) Create a linklist container
    • parameters:
      • iterable? Accepts an iterable as the initial value of the container

Attributes

  • size: number Linklist size

  • isEmpty: boolean Whether the linklist is empty

  • head: T View the linklist head element

  • tail: T View the linklist tail element

Methods

  • push(value: T): this Add an element to the tail of the linklist

    • parameters: - value: T The value to add
  • pop(): T Remove and return the tail element of the linklist

  • unshift(value: T): this Add an element to the head of the linklist

    • parameters: - value: T The value to add
  • shift(): T Remove and return the head element of the linklist

  • indexOf(value: T): number Get index by element

    • parameters: - value: T
  • at(index: number): T Get element by index

    • parameters: - index: number
  • removeAt(index: number): T Remove element by index

    • parameters: - index: number
  • forEach(callback: (value: T) => void): void Traverse the linklist with callbacks

    • parameters: - callback: (value: T) => void
  • values(): Generator<T> Returns a generator consisting of all elements

  • entries(): Generator<T> Returns a generator consisting of all key-value pairs

  • clear(): this Clear linklist

Package Sidebar

Install

npm i @dstl-js/linklist

Weekly Downloads

2

Version

1.0.0-beta.0

License

MIT

Unpacked Size

25.1 kB

Total Files

10

Last publish

Collaborators

  • liaoruikang