You can download the source code directly from the CDN and use it:
- https://unpkg.com/@dstl-js/linklist/dist/index.js
- https://unpkg.com/@dstl-js/linklist/dist/index.min.js
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
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
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
-
new LinkList(iterable?: Iterable<T>)
Create a linklist container-
parameters:
-
iterable?
Accepts an iterable as the initial value of the container
-
-
parameters:
-
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
-
push(value: T): this
Add an element to the tail of the linklist-
parameters: -
value: T
The value to add
-
parameters: -
-
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
-
parameters: -
-
shift(): T
Remove and return the head element of the linklist -
indexOf(value: T): number
Get index by element-
parameters: -
value: T
-
parameters: -
-
at(index: number): T
Get element by index-
parameters: -
index: number
-
parameters: -
-
removeAt(index: number): T
Remove element by index-
parameters: -
index: number
-
parameters: -
-
forEach(callback: (value: T) => void): void
Traverse the linklist with callbacks-
parameters: -
callback: (value: T) => void
-
parameters: -
-
values(): Generator<T>
Returns agenerator
consisting of all elements -
entries(): Generator<T>
Returns agenerator
consisting of all key-value pairs -
clear(): this
Clear linklist