A simply linked list data structure in JavaScript.
Big-O
| Access | Search | Insertion | Deletion
------- | ------ | ------ | --------- | --------
Average | Θ(n)
| Θ(n)
| Θ(1)
| Θ(1)
Worst | O(n)
| O(n)
| O(1)
| O(1)
Install
yarn add ds-linked-list
API
LinkedList
Class
insert
Insert an item at the end of the list.
Parameters
item
any
Examples
listlistlist
insertAfter
Insert an item after another one.
Parameters
ref
anyitem
any
Examples
listlistlist
insertAt
Insert an item at the given index.
Parameters
k
Numberitem
any
Examples
listlistlist
remove
Remove an item.
Parameters
item
any
Examples
listlistlist
has
Return true
if the item is in the list.
Parameters
item
any
Examples
list// => truelist// => truelist// => false
Returns Boolean
at
Return the item at the given index or -1
.
Parameters
k
Number
Examples
list// => 1list// => 'foo'list// => undefined
Returns any
entries
Return an array containing all the items.
Examples
list// => [ 1, 'foo', { foo: 'bar' }]
Returns Array
inspect
Return a string representation of the list.
Examples
list// => [ 1, 'foo', { foo: 'bar' }]
Returns String
length
Return the number of items in the list.
Examples
listlength// => 3
Returns Number
iterator
Iterate over the list.
Examples
for let item of list console// => 1// => 'foo'// => { foo: 'bar' }
License
MIT © Nicolas Gryman