High performance FIFO queue, O(1) time to push, shift, remove.
npm install link-list
var assert = require('assert')
var LinkList = require('link-list')
var list = new LinkList()
assert(list.isEmpty())
list.push(1)
assert(list.shift() === 1)
list.push(2)
list.push(3)
list.shiftEach(console.log) // => 2, 3
// Usage of LinkList.remove
list.push(1)
var obj = list.push(2)
list.push(3)
LinkList.remove(obj)
list.shiftEach(console.log) // => 1, 3
Create a LinkList instance.
Example:
var list = new LinkList()
Push data to the tail of queue, it will return a cancellable (reference of data), which could be used for removing data in O(1) time with remove
method.
Example:
var list = new LinkList()
assert(list.isEmpty())
var ref = list.push(1234)
list.remove(ref) // remove data in constant time
assert(list.isEmpty())
Shift data from the head of queue.
Example:
var list = new LinkList()
list.push(1)
list.push(2)
list.push(3)
list.shift() // => 1
list.shift() // => 2
list.shift() // => 3
Check if list is empty.
Example:
var list = new LinkList()
list.isEmpty() // => true
Remove data from queue in constant time.
Example:
var list = new LinkList()
var ref = list.push(123)
list.remove(ref)
list.isEmpty() // => true
Shift data from queue and invoke fn
with it until the end
Example:
var list = new LinkList()
list.push(1)
list.push(2)
list.push(3)
list.shiftEach(console.log) // => 1, 2, 3