Members
count
Returns the number of items in the list
- Source:
Methods
add(data, atIndex) → {Node}
Add element at the given position in the list. If no index is specified, data is added to the end of the list.
Returns the newly created node.
Runtime:
- O(1) for adding at start or end (default) of the list
- O(n) otherwise
Parameters:
Name | Type | Description |
---|---|---|
data |
any | |
atIndex |
number |
0 for adding at start of the list, or a positive integer to insert data in the middle. |
- Source:
Throws:
-
If index is negative or out of bounds
- Type
- OutOfBoundsError
Example
list.add(123) // Adds '123' at the end of the list
list.add(123, 0) // Adds '123' at the beginning of the list
list.add(123, 4) // Adds '123' at index 4 or throws an error
find(predicate) → (nullable) {any}
Search through the list for a node that matches the predicate.
Returns whatever the predicate returns. If predicate doesn't return a
non-null value for any Node, returns null
to indicate that the search
failed.
Parameters:
Name | Type | Description |
---|---|---|
predicate |
List.FindPredicate |
- Source:
indexOf(value) → (nullable) {number}
Finds the first occurrence of value
in the list.
Returns index of first occurrence or null
.
Runtime: O(n)
Parameters:
Name | Type | Description |
---|---|---|
value |
any |
- Source:
itemAtIndex(indexopt) → {any}
Get data stored at the given index. Runtime: O(n)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
index |
number |
<optional> |
0 |
- Source:
Throws:
remove(indexopt) → {any}
Removes list item at the given index and return the data held at the removed node. If no index is given, removes the last item.
Runtime:
- O(1) when removing from start or end (default)
- O(n) otherwise
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
index |
number |
<optional> |
last |
- Source:
Throws:
Example
list.remove(0) // Removes first item
list.remove(7) // Removes 8th item or throws an out of bounds error
list.remove() // Removes last item
Type Definitions
FindPredicate(node, index) → (nullable) {any}
This closure is used to specify custom search predicate used by the
find
method. The function is passed a node and its
index in the list on each iteration. Search continues until the closure
returns a value other than null
.
Parameters:
Name | Type | Description |
---|---|---|
node |
Node | |
index |
number |
- Source: