@jrc03c/data-structures
v0.0.2
Published
a little library of data structures i made for educational porpoises 🐬
Readme
intro
a little library of data structures i made for educational porpoises 🐬
installation
npm install --save @jrc03c/data-structuresusage
import { LinkedList } from "@jrc03c/data-structures"
const x = new LinkedList([1, 1, 2, 3, 5, 8, 13])
x.push(21)
x.push(34, 55, 89)
console.log(x.toArray())
// [
// 1, 1, 2, 3, 5,
// 8, 13, 21, 34, 55,
// 89
// ]api
LinkedList
in general, the LinkedList class uses the same api as Array but with a few small exceptions:
- the constructor only accepts an array of values (or nothing at all). in other words, it does not accept a length like the
Arrayconstructor does. - the class has
getandsetinstance methods. - the class does not have the static methods
fromAsyncandof. - the class does not have the instance methods
copyWithin,fill,flat, andflatMap. - in places where
Arraymethods returnArrayinstances (e.g.,filter), theLinkedListmethods returnLinkedListinstances. - in places where
Arraymethods acceptArrayinstances as arguments (e.g.,concat), theLinkedListmethods accept bothArrayinstances andLinkedListinstances.
LinkedList(x) (constructor)
optionally accepts an array of values with which to populate the list.
properties
length (getter)
returns the length of the list. is read-only.
methods
get(i)
returns the value at index i.
set(i, v)
sets the value at index i to be v.
examples
import { LinkedList } from "@jrc03c/data-structures"
const x = new LinkedList([2, 3, 4])
x.push("foo")
x.push(true, false)
console.log(x.toArray())
// [ 2, 3, 4, 'foo', true, false ]
const y = x.slice(1, 4)
console.log(y)
// LinkedList {}
console.log(y.toArray())
// [ 3, 4, 'foo' ]
y.splice(1, 1, "bar")
console.log(y.toArray())
// [ 3, 'bar', 'foo' ]
const z = x.concat([Infinity, NaN])
console.log(z.toArray())
// [ 2, 3, 4, 'foo', true, false, Infinity, NaN ]
for (const value of z) {
console.log(value)
}
// 2
// 3
// 4
// 'foo'
// true
// false
// Infinity
// NaN
const chars = new LinkedList(["a", "b", "c", "d", "e"])
console.log(chars.join(":"))
// 'a:b:c:d:e'
const fibs = new LinkedList([1, 1, 2, 3, 5, 8, 13])
console.log(fibs.reduce((a, b) => a + b))
// 33
fibs.sort((a, b) => b - a)
console.log(fibs.toArray())
// [
// 13, 8, 5, 3,
// 2, 1, 1
// ]