List

List

Linked List representation

Constructor

new List()

Example
const list = new Collections.LinkedList();
// FOR ALL EXAMPLES BELOW. ASSUME list IS CLEARED BEFORE EACH EXAMPLE

Methods

addToBack(data) → {List}

Adds the given data to right-most end of linked list
Example
list.addToBack("a")
 .addToBack("b"); // list is <"a", "b">
Parameters:
Name Type Description
data * the data to insert
Returns:
The instance this method was called
Type
List

addToFront(data) → {List}

Adds the given data to left-most end of linked list
Example
list.addToFront("a")
 .addToFront("b"); // list is <"b", "a">
Parameters:
Name Type Description
data * The data to insert
Returns:
The instance this method was called
Type
List

clear() → {undefined}

Empties the List
Returns:
Type
undefined

contains(data, comparator) → {number}

Returns whether the linked list contains the given data
Parameters:
Name Type Description
data * The data to insert into linked list
comparator function function to compare for equality
Returns:
The index of @param data or -1 if not found
Type
number

elementAtIndex(index) → {*|undefined}

Returns the data at given index
Example
list.addToFront("a")
 .addToFront("b")
 .addToFront("c");
const getSomething = list.elementAtIndex(2); // "a"
list.elementAtIndex(13); // undefined
Parameters:
Name Type Default Description
index number 0 The index to look at
Throws:
Will throw error if @param index is not number
Type
TypeError
Returns:
Index of element if @param index is in range or undefined
Type
* | undefined

every(predicate) → {boolean}

Reports if every element in the list passes a certain condition
Parameters:
Name Type Description
predicate function The function used for evaluations
Returns:
True if every element passes the test and false otherwise
Type
boolean

filter(predicate) → {List}

Returns a new list with only elements that return truthy when passed to the given callback
Parameters:
Name Type Description
predicate function The function used to evaluate elements
Returns:
A new list with filtered elements
Type
List

forEach(predicate) → {List}

Calls a callback function for each element in the list
Parameters:
Name Type Description
predicate function Function executed for each element (data, index)
Returns:
The instance that this method was called
Type
List

indexOf(data, comparator) → {number}

Returns the index of the given data in the linked list
Example
const customComparator = function(a, b) {
  if(a.age < b.age) { return -1;}
  else if(a.age > b.age) { return 1:}
  else { return 0; }
}
list.addToBack({ age : 2})
 .addToBack({ age : 3});
list.indexOf({ age : 2}, customComparator) // 0
Parameters:
Name Type Description
data * The data to find index of
comparator function function to compare for equality
Returns:
The index of @param data or -1 if not found
Type
number

insert(index, data) → {List}

Inserts given data into specific position in the linked list
Example
list.addToBack("a")
 .addToBack("b");
list.insert(1, "$");
// list is now <"a, "$, "b">
Parameters:
Name Type Default Description
index index 0 The index to insert data into
data * The data to insert into @param index
Throws:
Will throw error if @param index is not number
Type
TypeError
Returns:
- The instance this method was called
Type
List

remove(index) → {*|undefined}

Removes data at specific position in the linked list
Example
list.addToBack("a")
 .addToBack("b");
list.remove(1);
// list is now <"a">
Parameters:
Name Type Description
index index The index to insert data into
Throws:
Will throw error if @param index is not number
Type
TypeError
Returns:
The removed data or undefined if nothing removed
Type
* | undefined

removeBack() → {*|undefined}

Removes the right-most element in the linked list
Example
list.addToBack("a")
 .addToBack("b");
const removedData = list.removeBack(); // "b"
// list is now <"a">
Returns:
The removed data or undefined if nothing removed
Type
* | undefined

removeFront() → {*|undefined}

Removes the left-most element in the linked list
Example
list.addToBack("a")
 .addToBack("b");
const removedData = list.removeFront(); // "a"
// list is now <"b">
Returns:
The removed data or undefined if nothing removed
Type
* | undefined

size() → {number}

Returns the size of the List
Returns:
The size of the List
Type
number

some(predicate) → {boolean}

Reports if at least one element in the list passes a certain condition
Parameters:
Name Type Description
predicate function The function used for evaluations
Returns:
True if one or more elements passes the test and false otherwise
Type
boolean

toArray() → {Array}

Transforms a linked list to an array
Returns:
An array representation of 'this' List
Type
Array