Linked lists are a linear structure of items. Each item is a seperate object. Each item is made with a relationship to its next item.
sequenceDiagram
participant Node 1
participant Node 2
participant Node 3
Node 1-->>Node 2: next node
Node 2-->>Node 1: previous node
Node 2-->>Node 3: next node
Node 3-->>Node 2: previous node
item: { name: 'foo', data: { foo: 'bar' } },
nextItem: {
item: { name: 'bar', data: { biz: 'baz' } },
nextItem: null,
}
npm add @datastructures/linked-list -D
import { list } from '@datastructures/linked-list'
const items = [ { name: 'foo', data: { foo: 'bar'} }, { name: 'bar', data: { biz: 'baz', }}]
const tree = list.create(items)
// output
const arrayList = list.toArray(tree)
// array [ { name: 'foo', data: { foo: 'bar'} }, { name: 'bar', data: { biz: 'baz', }}]
// from here feel free to add other testable methods to satify your interviewer 🙋
Items are optional objects constructed using an item
function. Optionally, to create a list of items, an array of items can be passed into a list.create()
method.
name
: when adding a list a string
is always required
data
: a data {object}
for containing useful data with a Node
ex: const node = item('foo', { foo: "bar" }); const linkedList = list.create(node)
create(items)
creates a list from an array of items with a name
and an optional data
{object}
ex:
list.create([ { name: 'foo', data: { foo: 'bar'} }, { name: 'bar', data: { biz: 'baz', }}])
toArray(items)
creates an array of items with a name
and an optional data
{object}
from a list
ex:
list.toArray(someLinkedList)
Functional typed data structures offering structure clarity and simplicity.