Package Exports
- linked-list
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (linked-list) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
linked-list
Small double linked list.
Install
This package is ESM only: Node 12+ is needed to use it and it must be imported
instead of required.
npm:
npm install linked-listUse
import {List, Item} from 'linked-list'
var item1 = new Item()
var item2 = new Item()
var item3 = new Item()
var list = new List(item1, item2, item3)
list.head // => item1
list.head.next // => item2
list.head.next.next // => item3
list.head.next.prev // => item1
list.tail // => item3
list.tail.next // => `null`Subclassing:
import {List, Item} from 'linked-list'
class Tokens extends List {
join(delimiter) {
return this.toArray().join(delimiter)
}
}
class Token extends Item {
constructor(value) {
super()
this.value = value
}
toString() {
return this.value
}
}
var dogs = new Token('dogs')
var and = new Token('&')
var cats = new Token('cats')
var tokens = new Tokens(dogs, and, cats)
console.log(tokens.join(' ')) // => 'dogs & cats'
and.prepend(cats)
and.append(dogs)
console.log(tokens.join(' ') + '!') // => 'cats & dogs!'API
This package exports the following identifiers: List, Item.
There is no default export.
List([items…])
new List()
new List(new Item(), new Item())Create a new linked list.
List.from([items])
List.from()
List.from([])
List.from([new Item(), new Item()])Create a new this and adds the given array of items.
Ignores null or undefined values.
Throws an error when a given item has no detach, append, or prepend
methods.
List.of([items…])
List.of()
List.of(new Item(), new Item())Creates a new linked list from the given arguments.
Defers to List.from.
List#append(item)
var list = new List()
var item = new Item()
list.head === null // => true
item.list === null // => true
list.append(item)
list.head === item // => true
item.list === list // => trueAppends an item to a list.
Throws an error when the given item has no detach, append, or prepend
methods.
Returns the given item.
List#prepend(item)
var list = new List()
var item = new Item()
list.prepend(item)Prepends an item to a list.
Throws an error when the given item has no detach, append, or prepend
methods.
Returns the given item.
List#toArray()
var item1 = new Item()
var item2 = new Item()
var list = new List(item1, item2)
var array = list.toArray()
array[0] === item1 // => true
array[1] === item2 // => true
array[0].next === item2 // => true
array[1].prev === item1 // => trueReturns the items in the list in an array.
List#head
var item = new Item()
var list = new List(item)
list.head === item // => trueThe first item in a list, and null otherwise.
List#tail
var list = new List()
var item1 = new Item()
var item2 = new Item()
list.tail === null // => true
list.append(item1)
list.tail === null // => true, see note.
list.append(item2)
list.tail === item2 // => trueThe last item in a list, and null otherwise.
Note that a list with only one item has no tail, only a head.
List#size
var list = new List()
var item1 = new Item()
var item2 = new Item()
list.size === 0 // => true
list.append(item1)
list.size === 1 // => true
list.append(item2)
list.size === 2 // => trueThe number of items in the list.
Item()
var item = new Item()Creates a new linked list Item.
Item#append(item)
var item1 = new Item()
var item2 = new Item()
new List().append(item1)
item1.next === null // => true
item1.append(item2)
item1.next === item2 // => trueAdds the given item after the operated on item in a list.
Throws an error when the given item has no detach, append, or prepend
methods.
Returns false when the operated on item is not attached to a list, otherwise the
given item.
Item#prepend(item)
var item1 = new Item()
var item2 = new Item()
new List().append(item1)
item1.prev === null // => true
item1.prepend(item2)
item1.prev === item2 // => trueAdds the given item before the operated on item in a list.
Throws an error when the given item has no detach, append, or prepend
methods.
Returns false when the operated on item is not attached to a list, otherwise
the given item.
Item#detach()
var item = new Item()
var list = new List(item)
item.list === list // => true
item.detach()
item.list === null // => trueRemoves the operated on item from its parent list.
Removes references to it on its parent list, and prev and next items;
relinking them when possible.
Returns the operated on item.
Even when it was already detached.
Item#next
var item1 = new Item()
var item2 = new Item()
new List(item1)
item1.next === null // => true
item2.next === null // => true
item1.append(item2)
item1.next === item2 // => true
item1.detach()
item1.next === null // => trueThe items succeeding item, and null otherwise.
Item#prev
var item1 = new Item()
var item2 = new Item()
new List(item)
item1.prev === null // => true
item2.prev === null // => true
item1.append(item2)
item1.prev === item1 // => true
item2.detach()
item2.prev === null // => trueThe items preceding item, and null otherwise.
Item#list
var item = new Item()
var list = new List()
item.list === null // => true
list.append(item)
item.list === list // => true
item.detach()
item.list === null // => trueThe items parent list, and null otherwise.